Skip to content

Commit

Permalink
🦄: changed 'enforceZeros' option to 'preserveZeros'
Browse files Browse the repository at this point in the history
  • Loading branch information
whizkydee committed Aug 21, 2018
1 parent 13bcb77 commit a1b9897
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ reverse(/*...*/, {
reverse('of... unicorns', { invert: 'word' }); //=> unicorns of...
```

### `enforceZeros: {Boolean}`
### `preserveZeros: {Boolean}`

This property defaults to `false`. It specifies whether to enforce preceding zeros in the result of a number that contains trailing zeros. See [#3](https://github.com/whizkydee/type-reverse/issues/4) for more info.

```js
reverse(240, { enforceZeros: true }) //=> "042"
reverse(240, { preserveZeros: true }); //=> "042"
```

## `callback: {Function}`
Expand Down
12 changes: 7 additions & 5 deletions src/__tests__/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ describe('number reverse', () => {

test('reverse a number with trailing zeros and the option enabled', () => {
const number = 2000;
expect(reverse(number, { enforceZeros: true })).toBe('0002');
expect(reverse(number, { enforceZeros: true })).not.toBe(2);
expect(reverse(number, { preserveZeros: true })).toBe('0002');
expect(reverse(number, { preserveZeros: true })).not.toBe(2);
});

test('reverse a negative number with trailing zeros and the option enabled', () => {
const number = -2000;
expect(reverse(number, { enforceZeros: true, invert: 'sign' })).toEqual(
expect(reverse(number, { preserveZeros: true, invert: 'sign' })).toEqual(
'2000'
);
expect(reverse(number, { enforceZeros: true, invert: 'sign' })).not.toBe(2);
expect(reverse(number, { preserveZeros: true, invert: 'sign' })).not.toBe(
2
);
});

test('reverse a number without trailing zeros, but the option enabled', () => {
const number = 9999;
expect(reverse(number, { enforceZeros: true })).toEqual(9999);
expect(reverse(number, { preserveZeros: true })).toEqual(9999);
});

test('reverse a negative number without specifying options', () => {
Expand Down
16 changes: 8 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function reverse(input, options = {}, callback) {
options = !!!options ? new Object : options;

const newReversedArray = [...input].reverse(),
enforceZeros = options.enforceZeros,
preserveZeros = options.preserveZeros,
minusRE = /^-/;

if (input && !supported(input))
Expand All @@ -35,7 +35,7 @@ function reverse(input, options = {}, callback) {
);

options.invert = options.invert || 'index';
options.enforceZeros = enforceZeros || false;
options.preserveZeros = preserveZeros || false;
callback = callback || ((_, v) => v);

let result;
Expand All @@ -55,13 +55,13 @@ function reverse(input, options = {}, callback) {
const containsTrailingZeros = /0+$/.test(positiveNumberAsString);

// there's no need to stringify if...
// 1. the input contains trailing zeros, but the enforceZeros option is disabled
// 2. the input doesn't contain trailing zeros, but the enforceZeros option is enabled
// 3. the input doesn't contain trailing zeros, and the enforceZeros option is disabled
// 1. the input contains trailing zeros, but the preserveZeros option is disabled
// 2. the input doesn't contain trailing zeros, but the preserveZeros option is enabled
// 3. the input doesn't contain trailing zeros, and the preserveZeros option is disabled
const noNeedToStringify =
(containsTrailingZeros && !enforceZeros) ||
(!containsTrailingZeros && enforceZeros) ||
(!containsTrailingZeros && !enforceZeros);
(containsTrailingZeros && !preserveZeros) ||
(!containsTrailingZeros && preserveZeros) ||
(!containsTrailingZeros && !preserveZeros);

if (bigInt)
throw new TypeError(
Expand Down

0 comments on commit a1b9897

Please sign in to comment.