Initial add (split from @polkadot/api)
This commit is contained in:
12
.babelrc
Normal file
12
.babelrc
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"presets": [
|
||||
"env",
|
||||
"stage-0"
|
||||
],
|
||||
"plugins": [
|
||||
"transform-flow-strip-types",
|
||||
"transform-class-properties",
|
||||
"transform-object-rest-spread",
|
||||
"transform-runtime"
|
||||
]
|
||||
}
|
||||
1
.coveralls.yml
Normal file
1
.coveralls.yml
Normal file
@@ -0,0 +1 @@
|
||||
service_name: travis-pro
|
||||
10
.editorconfig
Normal file
10
.editorconfig
Normal file
@@ -0,0 +1,10 @@
|
||||
root = true
|
||||
[*]
|
||||
indent_style=space
|
||||
indent_size=2
|
||||
tab_width=2
|
||||
end_of_line=lf
|
||||
charset=utf-8
|
||||
trim_trailing_whitespace=true
|
||||
max_line_length=120
|
||||
insert_final_newline=true
|
||||
35
.eslintrc.json
Normal file
35
.eslintrc.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"env": {
|
||||
"browser": true,
|
||||
"mocha": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": [
|
||||
"semistandard",
|
||||
"plugin:flowtype/recommended"
|
||||
],
|
||||
"globals": {
|
||||
"expect": true
|
||||
},
|
||||
"parser": "babel-eslint",
|
||||
"plugins": [
|
||||
"flowtype"
|
||||
],
|
||||
"rules": {
|
||||
"curly": ["error", "all"],
|
||||
"newline-after-var": ["error", "always"],
|
||||
"no-alert": "error",
|
||||
"no-debugger": "error",
|
||||
"no-duplicate-imports": ["error", {
|
||||
"includeExports": true
|
||||
}],
|
||||
"object-curly-spacing": ["error", "always"],
|
||||
"object-property-newline": 0,
|
||||
"one-var-declaration-per-line": ["error", "always"],
|
||||
"padded-blocks": ["error", {
|
||||
"blocks": "never",
|
||||
"classes": "never",
|
||||
"switches": "never"
|
||||
}]
|
||||
}
|
||||
}
|
||||
10
.flowconfig
Normal file
10
.flowconfig
Normal file
@@ -0,0 +1,10 @@
|
||||
[ignore]
|
||||
|
||||
[include]
|
||||
|
||||
[libs]
|
||||
|
||||
[options]
|
||||
esproposal.decorators=ignore
|
||||
module.ignore_non_literal_requires=true
|
||||
unsafe.enable_getters_and_setters=true
|
||||
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
coverage/
|
||||
lib/
|
||||
node_modules/
|
||||
tmp/
|
||||
4
.istanbul.yml
Normal file
4
.istanbul.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
instrumentation:
|
||||
root: src
|
||||
extensions:
|
||||
- .js
|
||||
13
.npmignore
Normal file
13
.npmignore
Normal file
@@ -0,0 +1,13 @@
|
||||
coverage
|
||||
scripts
|
||||
src
|
||||
test
|
||||
tmp
|
||||
.babelrc
|
||||
.coveralls.yml
|
||||
.editorconfig
|
||||
.eslintrc.json
|
||||
.flowconfig
|
||||
.gitignore
|
||||
.istanbul.yml
|
||||
.travis.yml
|
||||
4
.travis.yml
Normal file
4
.travis.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "8"
|
||||
script: ./scripts/travis.sh
|
||||
6
LICENSE
Normal file
6
LICENSE
Normal file
@@ -0,0 +1,6 @@
|
||||
ISC License (ISC)
|
||||
Copyright 2017 Jaco Greeff
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
17
README.md
Normal file
17
README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Utilities
|
||||
|
||||
[](https://travis-ci.org/polkadot-js/util)
|
||||
[](https://coveralls.io/github/polkadot-js/util?branch=master)
|
||||
[](https://david-dm.org/polkadot-js/util)
|
||||
[](https://david-dm.org/polkadot-js/util#info=devDependencies)
|
||||
|
||||
## Introduction
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
Installation -
|
||||
|
||||
```
|
||||
npm install --save @polkadot/util
|
||||
```
|
||||
5267
package-lock.json
generated
Normal file
5267
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
69
package.json
Normal file
69
package.json
Normal file
@@ -0,0 +1,69 @@
|
||||
{
|
||||
"name": "@polkadot/util",
|
||||
"version": "0.2.0",
|
||||
"description": "A collection of useful utilities for @polkadot",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"Polkadot"
|
||||
],
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": ">=6.4"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/polkadot-js/util.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/polkadot-js/util/issues"
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/util#readme",
|
||||
"scripts": {
|
||||
"build": "npm run build:js",
|
||||
"build:js": "rimraf lib && babel src --out-dir lib --ignore *.spec.js,types.js",
|
||||
"check": "npm run check:flow && npm run check:lint",
|
||||
"check:flow": "flow check",
|
||||
"check:lint": "eslint src",
|
||||
"ci:makeshift": "makeshift",
|
||||
"ci:coveralls": "coveralls < coverage/lcov.info",
|
||||
"test": "cross-env NODE_ENV=test babel-istanbul cover _mocha 'src/*.spec.js' 'src/**/*.spec.js'"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.26.0",
|
||||
"babel-core": "^6.26.0",
|
||||
"babel-eslint": "^8.0.2",
|
||||
"babel-istanbul": "^0.12.2",
|
||||
"babel-plugin-transform-class-properties": "^6.24.1",
|
||||
"babel-plugin-transform-flow-strip-types": "^6.22.0",
|
||||
"babel-plugin-transform-object-rest-spread": "^6.26.0",
|
||||
"babel-plugin-transform-runtime": "^6.23.0",
|
||||
"babel-preset-env": "^1.6.1",
|
||||
"babel-preset-flow": "^6.23.0",
|
||||
"babel-preset-stage-0": "^6.24.1",
|
||||
"chai": "^4.1.2",
|
||||
"coveralls": "^3.0.0",
|
||||
"cross-env": "^5.1.1",
|
||||
"eslint": "^4.11.0",
|
||||
"eslint-config-semistandard": "^11.0.0",
|
||||
"eslint-config-standard": "^10.2.1",
|
||||
"eslint-plugin-flowtype": "^2.39.1",
|
||||
"eslint-plugin-import": "^2.8.0",
|
||||
"eslint-plugin-node": "^5.2.1",
|
||||
"eslint-plugin-promise": "^3.6.0",
|
||||
"eslint-plugin-standard": "^3.0.1",
|
||||
"flow-bin": "^0.59.0",
|
||||
"makeshift": "^1.1.0",
|
||||
"mocha": "^4.0.1",
|
||||
"rimraf": "^2.6.2",
|
||||
"sinon": "^4.1.2",
|
||||
"sinon-chai": "^2.14.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"babel-runtime": "^6.26.0"
|
||||
}
|
||||
}
|
||||
54
scripts/travis.sh
Normal file
54
scripts/travis.sh
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/bin/bash
|
||||
# ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
set -e
|
||||
|
||||
function setupGit () {
|
||||
REPO=$1
|
||||
|
||||
echo "Setting up GitHub config for $REPO"
|
||||
|
||||
git config push.default simple
|
||||
git config merge.ours.driver true
|
||||
git config user.name "Travis CI"
|
||||
git config user.email "$COMMIT_AUTHOR_EMAIL"
|
||||
git remote set-url origin https://${GH_TOKEN}@github.com/${REPO}.git > /dev/null 2>&1
|
||||
}
|
||||
|
||||
echo "Running code checks & build"
|
||||
|
||||
npm run check
|
||||
npm run build
|
||||
npm run test
|
||||
npm run ci:coveralls
|
||||
|
||||
# Pull requests and commits to other branches shouldn't try to deploy, just build to verify
|
||||
if [ "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_BRANCH" != "master" ]; then
|
||||
echo "Branch check completed"
|
||||
|
||||
exit 0
|
||||
fi
|
||||
|
||||
setupGit ${TRAVIS_REPO_SLUG}
|
||||
|
||||
if [ -n "$(git status --untracked-files=no --porcelain)" ]; then
|
||||
echo "Adding build artifacts"
|
||||
|
||||
git add .
|
||||
git commit -m "[CI Skip] Build artifacts"
|
||||
fi
|
||||
|
||||
echo "Publishing to npm"
|
||||
|
||||
npm run ci:makeshift
|
||||
npm --no-git-tag-version version
|
||||
npm version patch -m "[CI Skip] Version bump"
|
||||
npm publish
|
||||
|
||||
echo "Final push to GitHub"
|
||||
|
||||
git push --quiet origin HEAD:refs/heads/$TRAVIS_BRANCH > /dev/null 2>&1
|
||||
|
||||
echo "Release completed"
|
||||
|
||||
exit 0
|
||||
28
src/hex.js
Normal file
28
src/hex.js
Normal file
@@ -0,0 +1,28 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
function addHexPrefix (value: ?string): string {
|
||||
if (value && hasHexPrefix(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return `0x${value || ''}`;
|
||||
}
|
||||
|
||||
function hasHexPrefix (value: ?string): boolean {
|
||||
return !!(value && value.substr(0, 2) === '0x');
|
||||
}
|
||||
|
||||
function stripHexPrefix (value: ?string): string {
|
||||
if (value && hasHexPrefix(value)) {
|
||||
return value.substr(2);
|
||||
}
|
||||
|
||||
return value || '';
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addHexPrefix,
|
||||
hasHexPrefix,
|
||||
stripHexPrefix
|
||||
};
|
||||
67
src/hex.spec.js
Normal file
67
src/hex.spec.js
Normal file
@@ -0,0 +1,67 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const { addHexPrefix, hasHexPrefix, stripHexPrefix } = require('./hex');
|
||||
|
||||
describe('hex', () => {
|
||||
describe('addHexPrefix', () => {
|
||||
it('does not add when prefix is available', () => {
|
||||
expect(
|
||||
addHexPrefix('0x123')
|
||||
).to.equal('0x123');
|
||||
});
|
||||
|
||||
it('adds the prefix when it is not available', () => {
|
||||
expect(
|
||||
addHexPrefix('123')
|
||||
).to.equal('0x123');
|
||||
});
|
||||
|
||||
it('returns null as 0x', () => {
|
||||
expect(
|
||||
addHexPrefix(null)
|
||||
).to.equal('0x');
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasHexPrefix', () => {
|
||||
it('returns true when hex prefix is found', () => {
|
||||
expect(
|
||||
hasHexPrefix('0x123')
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false when no prefix attached', () => {
|
||||
expect(
|
||||
hasHexPrefix('123')
|
||||
).to.be.false;
|
||||
});
|
||||
|
||||
it('returns false when null value supplied', () => {
|
||||
expect(
|
||||
hasHexPrefix(null)
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('stripHexPrefix', () => {
|
||||
it('returns the value as-is when no prefix', () => {
|
||||
expect(
|
||||
stripHexPrefix('01ab')
|
||||
).to.equal('01ab');
|
||||
});
|
||||
|
||||
it('returns an empty string when null value supplied', () => {
|
||||
expect(
|
||||
stripHexPrefix(null)
|
||||
).to.equal('');
|
||||
});
|
||||
|
||||
it('strips the prefix from other strings', () => {
|
||||
expect(
|
||||
stripHexPrefix('0x1223')
|
||||
).to.equal('1223');
|
||||
});
|
||||
});
|
||||
});
|
||||
20
src/is.js
Normal file
20
src/is.js
Normal file
@@ -0,0 +1,20 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
function isFunction (value: any): boolean {
|
||||
return typeof value === 'function';
|
||||
}
|
||||
|
||||
function isNumber (value: any): boolean {
|
||||
return typeof value === 'number';
|
||||
}
|
||||
|
||||
function isUndefined (value: any): boolean {
|
||||
return typeof value === 'undefined';
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
isFunction,
|
||||
isNumber,
|
||||
isUndefined
|
||||
};
|
||||
49
src/is.spec.js
Normal file
49
src/is.spec.js
Normal file
@@ -0,0 +1,49 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const { isFunction, isNumber, isUndefined } = require('./is');
|
||||
|
||||
describe('is', () => {
|
||||
describe('isFunction', () => {
|
||||
it('returns true on valid functions', () => {
|
||||
expect(
|
||||
isFunction(isFunction)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false of invalid functions', () => {
|
||||
expect(
|
||||
isFunction('notAFunction')
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isNumber', () => {
|
||||
it('returns true on valid numbers', () => {
|
||||
expect(
|
||||
isNumber(2)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns fals on invalid numbers', () => {
|
||||
expect(
|
||||
isNumber('2')
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isUndefined', () => {
|
||||
it('returns true on undefined values', () => {
|
||||
expect(
|
||||
isUndefined()
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false on defined values', () => {
|
||||
expect(
|
||||
isUndefined(null)
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
});
|
||||
8
test/mocha.config.js
Normal file
8
test/mocha.config.js
Normal file
@@ -0,0 +1,8 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const chai = require('chai');
|
||||
const sinonChai = require('sinon-chai');
|
||||
|
||||
chai.use(sinonChai);
|
||||
|
||||
global.expect = chai.expect;
|
||||
1
test/mocha.opts
Normal file
1
test/mocha.opts
Normal file
@@ -0,0 +1 @@
|
||||
-r ./test/mocha.config
|
||||
Reference in New Issue
Block a user