Skip to content

Commit

Permalink
feat(sanketa): Add sign of numbers 💯
Browse files Browse the repository at this point in the history
  • Loading branch information
ideyuta committed Apr 27, 2016
1 parent 19e841a commit 3556408
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @param {Object} [opts] - options
* @param {boolean} [opts.reverse] - reverse flag
* @param {number|string} [opts.sepalater] - sepalater
* @param {boolean} [opts.signOfNumber] - if true, put a signed numbers
* @return {string}
*/
export default function sanketa(rawChars: (number|string), opts: Object = {}): string {
Expand All @@ -14,7 +15,7 @@ export default function sanketa(rawChars: (number|string), opts: Object = {}): s
let chars = rawChars;
let snum = 0;

chars = typeof chars === 'number' ? chars.toString() : chars;
chars = typeof chars === 'number' ? Math.abs(chars).toString() : chars;
chars = opts.reverse ? chars : reverse(chars);
chars = Array.from(chars).map((c, key, map) => {
if (key < map.length - 1) {
Expand All @@ -28,7 +29,16 @@ export default function sanketa(rawChars: (number|string), opts: Object = {}): s
}
return c;
}).join('');
return opts.reverse ? chars : reverse(chars);

let results = opts.reverse ? chars : reverse(chars);
if (typeof rawChars === 'number') {
if (opts.signOfNumber) {
results = rawChars < 0 ? `-${results}` : `+${results}`;
} else {
results = rawChars < 0 ? `-${results}` : results;
}
}
return results;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,28 @@ describe('sanketa', () => {
assert(sanketa('characters', {sepalater: ['🍣', '🍵']}) === 'c🍣har🍵act🍣ers');
});
});

describe('should return number of signed', () => {
it('flag is true', () => {
assert(sanketa(123, {signOfNumber: true}) === '+123');
assert(sanketa(-123, {signOfNumber: true}) === '-123');
assert(sanketa(12345, {signOfNumber: true}) === '+12,345');
assert(sanketa(-12345, {signOfNumber: true}) === '-12,345');
});

it('flag is false', () => {
assert(sanketa(123) === '123');
assert(sanketa(-123) === '-123');
assert(sanketa(12345) === '12,345');
assert(sanketa(-12345) === '-12,345');
});
});

it('if typeof string should not return number of signed', () => {
assert(sanketa('123', {signOfNumber: true}) === '123');
assert(sanketa('-123', {signOfNumber: true}) === '-,123');
assert(sanketa('12345', {signOfNumber: true}) === '12,345');
assert(sanketa('-12345', {signOfNumber: true}) === '-12,345');
});

});

0 comments on commit 3556408

Please sign in to comment.