binary-case
Advanced tools
Comparing version 1.0.0 to 1.1.0
78
index.js
@@ -29,9 +29,55 @@ /** | ||
function binaryCase(string, number, options) { | ||
const binary = (number >>> 0).toString(2); | ||
if (!options || typeof options !== 'object') options = {}; | ||
if (!options.hasOwnProperty('allowOverflow')) options.allowOverflow = true; | ||
if (binary.length > string.match(/[a-z]/ig).length && !options.allowOverflow) return false; | ||
if (number > binaryCase.maxNumber(string) && !options.allowOverflow) return false; | ||
return getBinaryCase(string, number); | ||
} | ||
binaryCase.iterator = function(string, options) { | ||
const max = binaryCase.maxNumber(string); | ||
if (!options || typeof options !== 'object') options = {}; | ||
if (!options.hasOwnProperty('startIndex')) options.startIndex = 0; | ||
if (typeof options.startIndex !== 'number' || !Number.isInteger(options.startIndex) && options.startIndex < 0) throw Error('Option startIndex must be a non-negative integer.'); | ||
var index = options && typeof options === 'object' ? options.startIndex || 0 : 0; | ||
return { | ||
next: function() { | ||
return index > max | ||
? { done: true } | ||
: { done: false, value: getBinaryCase(string, index++) }; | ||
} | ||
}; | ||
}; | ||
/** | ||
* Get the maximum number that can be used before causing overflow. | ||
* @param {string} string | ||
* @returns {number} | ||
*/ | ||
binaryCase.maxNumber = function(string) { | ||
const pow = string.match(/[a-z]/ig).length; | ||
return Math.pow(2, pow) - 1; | ||
}; | ||
/** | ||
* Get an array of all possible variations. | ||
* @param {string} string | ||
* @returns {string[]} | ||
*/ | ||
binaryCase.variations = function(string) { | ||
const results = []; | ||
const max = binaryCase.maxNumber(string); | ||
for (var i = 0; i <= max; i++) { | ||
results.push(binaryCase(string, i)); | ||
} | ||
return results; | ||
}; | ||
function getBinaryCase(string, number) { | ||
const binary = (number >>> 0).toString(2); | ||
var bin; | ||
@@ -64,26 +110,2 @@ var ch; | ||
return result; | ||
} | ||
/** | ||
* Get the maximum number that can be used before causing overflow. | ||
* @param {string} string | ||
* @returns {number} | ||
*/ | ||
binaryCase.maxNumber = function(string) { | ||
const pow = string.match(/[a-z]/ig).length; | ||
return Math.pow(2, pow) - 1; | ||
}; | ||
/** | ||
* Get an array of all possible variations. | ||
* @param {string} string | ||
* @returns {string[]} | ||
*/ | ||
binaryCase.variations = function(string) { | ||
const results = []; | ||
const max = binaryCase.maxNumber(string); | ||
for (var i = 0; i <= max; i++) { | ||
results.push(binaryCase(string, i)); | ||
} | ||
return results; | ||
}; | ||
} |
{ | ||
"name": "binary-case", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Take a string and a number and perform binary case switching on alpha characters.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "nyc tape test.js && nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
@@ -25,4 +25,6 @@ "repository": { | ||
"devDependencies": { | ||
"coveralls": "^2.11.14", | ||
"nyc": "^8.4.0", | ||
"tape": "^4.6.2" | ||
} | ||
} |
@@ -0,1 +1,6 @@ | ||
[![NPM version](https://img.shields.io/npm/v/binary-case.svg?style=flat)](https://npmjs.org/package/binary-case) | ||
[![NPM downloads](https://img.shields.io/npm/dm/binary-case.svg?style=flat)](https://npmjs.org/package/binary-case) | ||
[![Build status](https://img.shields.io/travis/Gi60s/binary-case.svg?style=flat)](https://travis-ci.org/Gi60s/binary-case) | ||
[![Test coverage](https://img.shields.io/coveralls/Gi60s/binary-case.svg?style=flat)](https://coveralls.io/r/Gi60s/binary-case?branch=master) | ||
# binary-case | ||
@@ -5,3 +10,3 @@ | ||
**Example** | ||
## Example | ||
@@ -8,0 +13,0 @@ ```js |
16
test.js
@@ -73,2 +73,18 @@ /** | ||
const iterator = binaryCase.iterator('abc', { allowOverflow: false }); | ||
const first = iterator.next(); | ||
t.equal(first.value, 'abc', 'no change'); | ||
t.equal(first.done, false, 'not done'); | ||
t.equal(iterator.next().value, 'Abc', 'first char'); | ||
t.equal(iterator.next().value, 'aBc', 'second char'); | ||
t.equal(iterator.next().value, 'ABc', 'first and second char'); | ||
t.equal(iterator.next().value, 'abC', 'third char'); | ||
t.equal(iterator.next().value, 'AbC', 'first and third char'); | ||
t.equal(iterator.next().value, 'aBC', 'second and third char'); | ||
const last = iterator.next(); | ||
t.equal(last.value, 'ABC', 'all chars'); | ||
t.equal(last.done, false, 'is not done'); | ||
t.equal(iterator.next().done, true, 'done'); | ||
t.equal(iterator.next().done, true, 'still done'); | ||
const variations = binaryCase.variations('abc'); | ||
@@ -75,0 +91,0 @@ t.equal(variations.length, 8, 'number of variations'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
36946
13
214
0
75
3