Initial add (split from @polkadot/api)
This commit is contained in:
+28
@@ -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
|
||||
};
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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
|
||||
};
|
||||
@@ -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;
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user