Skip to content

Commit

Permalink
Merge branch 'master' into console-to-debug
Browse files Browse the repository at this point in the history
  • Loading branch information
bonustrack authored Jun 27, 2017
2 parents 753994c + a21d3b4 commit bcc9c41
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 54 deletions.
15 changes: 14 additions & 1 deletion doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -920,4 +920,17 @@ console.log(reputation);
```
var steemPower = steem.formatter.vestToSteem(vestingShares, totalVestingShares, totalVestingFundSteem);
console.log(steemPower);
```
```

# Utils

### Validate Username
```
var isValidUsername = steem.utils.validateAccountName('test1234');
console.log(isValidUsername);
// => 'null'
var isValidUsername = steem.utils.validateAccountName('a1');
console.log(isValidUsername);
// => 'Account name should be longer.'
```
3 changes: 2 additions & 1 deletion examples/get-post-content.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const steem = require('..');
const steem = require('../lib');

const resultP = steem.api.getContentAsync('yamadapc', 'test-1-2-3-4-5-6-7-9');
resultP.then(result => console.log(result));
4 changes: 2 additions & 2 deletions examples/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var steem = require('./../index');
var steem = require('../lib');

steem.api.getAccountCount(function(err, result) {
console.log(err, result);
Expand Down Expand Up @@ -32,4 +32,4 @@ steem.api.getDiscussionsByActive({
start_permlink: 'this-week-in-level-design-1-22-2017'
}, function(err, result) {
console.log(err, result);
});
});
2 changes: 1 addition & 1 deletion examples/test-vote.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const steem = require('..');
const steem = require('../lib');

const username = process.env.STEEM_USERNAME;
const password = process.env.STEEM_PASSWORD;
Expand Down
15 changes: 0 additions & 15 deletions index.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "steem",
"version": "0.5.18",
"description": "Steem.js the JavaScript API for Steem blockchain",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"test": "eslint --quiet src test; mocha -t 20000 --require babel-polyfill --require babel-register",
"test-auth": "npm test -- --grep 'steem.auth'",
Expand Down
2 changes: 1 addition & 1 deletion src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import isNode from 'detect-node';
import newDebug from 'debug';
import config from '../config';
import methods from './methods';
import { camelCase } from '../util';
import { camelCase } from '../utils';

const debugEmitters = newDebug('steem:emitters');
const debugProtocol = newDebug('steem:protocol');
Expand Down
2 changes: 1 addition & 1 deletion src/broadcast/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import formatterFactory from '../formatter';
import operations from './operations.json';
import steemApi from '../api';
import steemAuth from '../auth';
import { camelCase } from '../util';
import { camelCase } from '../utils';

const debug = newDebug('steem:broadcast');
const formatter = formatterFactory(steemApi);
Expand Down
4 changes: 3 additions & 1 deletion src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ const auth = require("./auth");
const broadcast = require("./broadcast");
const config = require("./config");
const formatter = require("./formatter")(api);
const utils = require("./utils");

const steem = {
api,
auth,
broadcast,
config,
formatter
formatter,
utils
};

if (typeof window !== "undefined") {
Expand Down
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const api = require('./api');
const auth = require('./auth');
const broadcast = require('./broadcast');
const formatter = require('./formatter')(api);
const memo = require('./auth/memo');
const config = require('./config');
const utils = require('./utils');

module.exports = {
api,
auth,
broadcast,
formatter,
memo,
config,
utils,
};
6 changes: 0 additions & 6 deletions src/util.js

This file was deleted.

45 changes: 45 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const snakeCaseRe = /_([a-z])/g;
export function camelCase(str) {
return str.replace(snakeCaseRe, function(_m, l) {
return l.toUpperCase();
});
}

export function validateAccountName(value) {
let i, label, len, suffix;

suffix = "Account name should ";
if (!value) {
return suffix + "not be empty.";
}
const length = value.length;
if (length < 3) {
return suffix + "be longer.";
}
if (length > 16) {
return suffix + "be shorter.";
}
if (/\./.test(value)) {
suffix = "Each account segment should ";
}
const ref = value.split(".");
for (i = 0, len = ref.length; i < len; i++) {
label = ref[i];
if (!/^[a-z]/.test(label)) {
return suffix + "start with a letter.";
}
if (!/^[a-z0-9-]*$/.test(label)) {
return suffix + "have only letters, digits, or dashes.";
}
if (/--/.test(label)) {
return suffix + "have only one dash in a row.";
}
if (!/[a-z0-9]$/.test(label)) {
return suffix + "end with a letter or digit.";
}
if (!(label.length >= 3)) {
return suffix + "be longer";
}
}
return null;
}
36 changes: 17 additions & 19 deletions test/broadcast.test.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
import Promise from 'bluebird';
import should from 'should';
import steemAuth from '../src/auth';
import steemBroadcast from '../src/broadcast';
import packageJson from '../package.json';
import steem from '../src';

const username = process.env.STEEM_USERNAME || 'guest123';
const password = process.env.STEEM_PASSWORD;
const postingWif = password
? steemAuth.toWif(username, password, 'posting')
? steem.auth.toWif(username, password, 'posting')
: '5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg';

describe('steem.broadcast:', () => {
it('exists', () => {
should.exist(steemBroadcast);
should.exist(steem.broadcast);
});

it('has generated methods', () => {
should.exist(steemBroadcast.vote);
should.exist(steemBroadcast.voteWith);
should.exist(steemBroadcast.comment);
should.exist(steemBroadcast.transfer);
should.exist(steem.broadcast.vote);
should.exist(steem.broadcast.voteWith);
should.exist(steem.broadcast.comment);
should.exist(steem.broadcast.transfer);
});

it('has backing methods', () => {
should.exist(steemBroadcast.send);
should.exist(steem.broadcast.send);
});

it('has promise methods', () => {
should.exist(steemBroadcast.sendAsync);
should.exist(steemBroadcast.voteAsync);
should.exist(steemBroadcast.transferAsync);
should.exist(steem.broadcast.sendAsync);
should.exist(steem.broadcast.voteAsync);
should.exist(steem.broadcast.transferAsync);
});

describe('patching transaction with default global properties', () => {
it('works', async () => {
const tx = await steemBroadcast._prepareTransaction({
const tx = await steem.broadcast._prepareTransaction({
extensions: [],
operations: [['vote', {
voter: 'yamadapc',
Expand All @@ -55,7 +53,7 @@ describe('steem.broadcast:', () => {

describe('downvoting', () => {
it('works', async () => {
const tx = await steemBroadcast.voteAsync(
const tx = await steem.broadcast.voteAsync(
postingWif,
username,
'yamadapc',
Expand All @@ -80,7 +78,7 @@ describe('steem.broadcast:', () => {
});

it('works', async () => {
const tx = await steemBroadcast.voteAsync(
const tx = await steem.broadcast.voteAsync(
postingWif,
username,
'yamadapc',
Expand All @@ -99,12 +97,12 @@ describe('steem.broadcast:', () => {
});

it('works with callbacks', (done) => {
steemBroadcast.vote(
steem.broadcast.vote(
postingWif,
username,
'yamadapc',
'test-1-2-3-4-5-6-7-9',
10000,
5000,
(err, tx) => {
if (err) return done(err);
tx.should.have.properties([
Expand All @@ -127,7 +125,7 @@ describe('steem.broadcast:', () => {
});

it('works', async () => {
const tx = await steemBroadcast.customJsonAsync(
const tx = await steem.broadcast.customJsonAsync(
postingWif,
[],
[username],
Expand Down
9 changes: 4 additions & 5 deletions test/comment.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import Promise from 'bluebird';
import should from 'should';
import steemAuth from '../src/auth';
import steemBroadcast from '../src/broadcast';
import steem from '../src';
import pkg from '../package.json';

const username = process.env.STEEM_USERNAME || 'guest123';
const password = process.env.STEEM_PASSWORD;
const postingWif = password
? steemAuth.toWif(username, password, 'posting')
? steem.auth.toWif(username, password, 'posting')
: '5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg';

describe('steem.broadcast:', () => {
Expand All @@ -18,7 +17,7 @@ describe('steem.broadcast:', () => {
});

it('works', async () => {
const permlink = new Date().toISOString().replace(/[^a-zA-Z0-9]+/g, '').toLowerCase();
const permlink = steem.formatter.commentPermlink('siol', 'test');
const operations = [
['comment',
{
Expand Down Expand Up @@ -52,7 +51,7 @@ describe('steem.broadcast:', () => {
}]
];

const tx = await steemBroadcast.sendAsync(
const tx = await steem.broadcast.sendAsync(
{ operations, extensions: [] },
{ posting: postingWif }
);
Expand Down

0 comments on commit bcc9c41

Please sign in to comment.