diff --git a/readme.md b/readme.md index f1d96de..ceafd6c 100644 --- a/readme.md +++ b/readme.md @@ -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}` diff --git a/src/__tests__/numbers.js b/src/__tests__/numbers.js index bd34839..66a6018 100644 --- a/src/__tests__/numbers.js +++ b/src/__tests__/numbers.js @@ -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', () => { diff --git a/src/index.js b/src/index.js index bef7918..b5e5dc3 100644 --- a/src/index.js +++ b/src/index.js @@ -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)) @@ -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; @@ -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(