fast-unique-numbers
Advanced tools
Comparing version 2.1.1 to 2.1.2
@@ -12,48 +12,59 @@ (function (global, factory) { | ||
*/ | ||
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; | ||
var cache = function cache(collection, nextNumber) { | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
}; | ||
var generateUniqueNumber = function generateUniqueNumber(collection) { | ||
var lastNumber = LAST_NUMBER_WEAK_MAP.get(collection); | ||
/* | ||
* Let's try the cheapest algorithm first. It might fail to produce a new | ||
* number, but it is so cheap that it is okay to take the risk. Just | ||
* increase the last number by one or reset it to 0 if we reached the upper | ||
* bound of SMIs (which stands for small integers). When the last number is | ||
* unknown it is assumed that the collection contains zero based consecutive | ||
* numbers. | ||
*/ | ||
var nextNumber = lastNumber === undefined ? collection.size : lastNumber > 2147483648 ? 0 : lastNumber + 1; | ||
if (!collection.has(nextNumber)) { | ||
return cache(collection, nextNumber); | ||
} | ||
/* | ||
* If there are less than half of 2 ** 31 numbers stored in the collection, | ||
* the chance to generate a new random number in the range from 0 to 2 ** 31 | ||
* is at least 50%. It's benifitial to use only SMIs because they perform | ||
* much better in any environment based on V8. | ||
*/ | ||
if (collection.size < 1073741824) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * 2147483648); | ||
} | ||
return cache(collection, nextNumber); | ||
} | ||
// Quickly check if there is a theoretical chance to generate a new number. | ||
if (collection.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!'); | ||
} | ||
// Otherwise use the full scale of safely usable integers. | ||
var lastNumber = LAST_NUMBER_WEAK_MAP.get(collection); | ||
/* | ||
* Let's try the cheapest algorithm first. It might fail to produce a new | ||
* number, but it is so cheap that it is okay to take the risk. Just | ||
* increase the last number by one or reset it to 0 if we reached the upper | ||
* bound of SMIs (which stands for small integers). When the last number is | ||
* unknown it is assumed that the collection contains zero based consecutive | ||
* numbers. | ||
*/ | ||
var nextNumber = lastNumber === undefined ? collection.size : lastNumber > 2147483648 ? 0 : lastNumber + 1; | ||
if (!collection.has(nextNumber)) { | ||
return cache(collection, nextNumber); | ||
} | ||
/* | ||
* If there are less than half of 2 ** 31 numbers stored in the collection, | ||
* the chance to generate a new random number in the range from 0 to 2 ** 31 | ||
* is at least 50%. It's benifitial to use only SMIs because they perform | ||
* much better in any environment based on V8. | ||
*/ | ||
if (collection.size < 1073741824) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER); | ||
nextNumber = Math.floor(Math.random() * 2147483648); | ||
} | ||
return cache(collection, nextNumber); | ||
} // Quickly check if there is a theoretical chance to generate a new number. | ||
if (collection.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!'); | ||
} // Otherwise use the full scale of safely usable integers. | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER); | ||
} | ||
return cache(collection, nextNumber); | ||
}; | ||
var addUniqueNumber = function addUniqueNumber(set) { | ||
var number = generateUniqueNumber(set); | ||
set.add(number); | ||
return number; | ||
var number = generateUniqueNumber(set); | ||
set.add(number); | ||
return number; | ||
}; | ||
@@ -60,0 +71,0 @@ |
@@ -1,15 +0,16 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.addUniqueNumber = undefined; | ||
exports.addUniqueNumber = void 0; | ||
var _generateUniqueNumber = require('./generate-unique-number'); | ||
var _generateUniqueNumber = require("./generate-unique-number"); | ||
const addUniqueNumber = exports.addUniqueNumber = set => { | ||
const number = (0, _generateUniqueNumber.generateUniqueNumber)(set); | ||
set.add(number); | ||
return number; | ||
const addUniqueNumber = set => { | ||
const number = (0, _generateUniqueNumber.generateUniqueNumber)(set); | ||
set.add(number); | ||
return number; | ||
}; | ||
//# sourceMappingURL=/build/es2015/helpers/add-unique-number.js.map | ||
exports.addUniqueNumber = addUniqueNumber; |
@@ -1,6 +0,7 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.generateUniqueNumber = void 0; | ||
const LAST_NUMBER_WEAK_MAP = new WeakMap(); | ||
@@ -11,43 +12,55 @@ /* | ||
*/ | ||
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; | ||
const cache = (collection, nextNumber) => { | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
}; | ||
const generateUniqueNumber = exports.generateUniqueNumber = collection => { | ||
const lastNumber = LAST_NUMBER_WEAK_MAP.get(collection); | ||
/* | ||
* Let's try the cheapest algorithm first. It might fail to produce a new | ||
* number, but it is so cheap that it is okay to take the risk. Just | ||
* increase the last number by one or reset it to 0 if we reached the upper | ||
* bound of SMIs (which stands for small integers). When the last number is | ||
* unknown it is assumed that the collection contains zero based consecutive | ||
* numbers. | ||
*/ | ||
let nextNumber = lastNumber === undefined ? collection.size : lastNumber > 2147483648 ? 0 : lastNumber + 1; | ||
if (!collection.has(nextNumber)) { | ||
return cache(collection, nextNumber); | ||
} | ||
/* | ||
* If there are less than half of 2 ** 31 numbers stored in the collection, | ||
* the chance to generate a new random number in the range from 0 to 2 ** 31 | ||
* is at least 50%. It's benifitial to use only SMIs because they perform | ||
* much better in any environment based on V8. | ||
*/ | ||
if (collection.size < 1073741824) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * 2147483648); | ||
} | ||
return cache(collection, nextNumber); | ||
} | ||
// Quickly check if there is a theoretical chance to generate a new number. | ||
if (collection.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!'); | ||
} | ||
// Otherwise use the full scale of safely usable integers. | ||
const generateUniqueNumber = collection => { | ||
const lastNumber = LAST_NUMBER_WEAK_MAP.get(collection); | ||
/* | ||
* Let's try the cheapest algorithm first. It might fail to produce a new | ||
* number, but it is so cheap that it is okay to take the risk. Just | ||
* increase the last number by one or reset it to 0 if we reached the upper | ||
* bound of SMIs (which stands for small integers). When the last number is | ||
* unknown it is assumed that the collection contains zero based consecutive | ||
* numbers. | ||
*/ | ||
let nextNumber = lastNumber === undefined ? collection.size : lastNumber > 2147483648 ? 0 : lastNumber + 1; | ||
if (!collection.has(nextNumber)) { | ||
return cache(collection, nextNumber); | ||
} | ||
/* | ||
* If there are less than half of 2 ** 31 numbers stored in the collection, | ||
* the chance to generate a new random number in the range from 0 to 2 ** 31 | ||
* is at least 50%. It's benifitial to use only SMIs because they perform | ||
* much better in any environment based on V8. | ||
*/ | ||
if (collection.size < 1073741824) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER); | ||
nextNumber = Math.floor(Math.random() * 2147483648); | ||
} | ||
return cache(collection, nextNumber); | ||
} // Quickly check if there is a theoretical chance to generate a new number. | ||
if (collection.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!'); | ||
} // Otherwise use the full scale of safely usable integers. | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER); | ||
} | ||
return cache(collection, nextNumber); | ||
}; | ||
//# sourceMappingURL=/build/es2015/helpers/generate-unique-number.js.map | ||
exports.generateUniqueNumber = generateUniqueNumber; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,10 +6,17 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.generateUniqueNumber = exports.addUniqueNumber = undefined; | ||
Object.defineProperty(exports, "addUniqueNumber", { | ||
enumerable: true, | ||
get: function () { | ||
return _addUniqueNumber.addUniqueNumber; | ||
} | ||
}); | ||
Object.defineProperty(exports, "generateUniqueNumber", { | ||
enumerable: true, | ||
get: function () { | ||
return _generateUniqueNumber.generateUniqueNumber; | ||
} | ||
}); | ||
var _addUniqueNumber = require('./helpers/add-unique-number'); | ||
var _addUniqueNumber = require("./helpers/add-unique-number"); | ||
var _generateUniqueNumber = require('./helpers/generate-unique-number'); | ||
exports.addUniqueNumber = _addUniqueNumber.addUniqueNumber; | ||
exports.generateUniqueNumber = _generateUniqueNumber.generateUniqueNumber; | ||
//# sourceMappingURL=/build/es2015/module.js.map | ||
var _generateUniqueNumber = require("./helpers/generate-unique-number"); |
@@ -13,3 +13,2 @@ { | ||
"dependencies": { | ||
"babel-runtime": "^6.26.0", | ||
"tslib": "^1.9.3" | ||
@@ -19,10 +18,10 @@ }, | ||
"devDependencies": { | ||
"@commitlint/cli": "^7.0.0", | ||
"@commitlint/config-angular": "^7.0.1", | ||
"babel-core": "^6.26.3", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-register": "^6.26.0", | ||
"@babel/core": "^7.0.0", | ||
"@babel/plugin-external-helpers": "^7.0.0", | ||
"@babel/plugin-transform-runtime": "^7.0.0", | ||
"@babel/preset-env": "^7.0.0", | ||
"@babel/register": "^7.0.0", | ||
"@babel/runtime": "^7.0.0", | ||
"@commitlint/cli": "^7.1.1", | ||
"@commitlint/config-angular": "^7.1.1", | ||
"benchmark": "^2.1.4", | ||
@@ -33,6 +32,6 @@ "chai": "^4.1.2", | ||
"eslint": "^5.4.0", | ||
"eslint-config-holy-grail": "^30.0.0", | ||
"eslint-config-holy-grail": "^31.0.0", | ||
"greenkeeper-lockfile": "^1.15.1", | ||
"grunt": "^1.0.3", | ||
"grunt-babel": "^7.0.0", | ||
"grunt-babel": "^8.0.0-beta.0", | ||
"grunt-cli": "^1.3.1", | ||
@@ -58,10 +57,10 @@ "grunt-contrib-clean": "^1.1.0", | ||
"mocha": "^5.2.0", | ||
"rollup": "^0.64.1", | ||
"rollup-plugin-babel": "^3.0.7", | ||
"rollup": "^0.65.0", | ||
"rollup-plugin-babel": "^4.0.2", | ||
"sinon": "^6.1.5", | ||
"sinon-chai": "^3.2.0", | ||
"ts-loader": "^4.5.0", | ||
"tsconfig-holy-grail": "^6.0.0", | ||
"tsconfig-holy-grail": "^6.0.3", | ||
"tslint": "^5.11.0", | ||
"tslint-config-holy-grail": "^33.0.12", | ||
"tslint-config-holy-grail": "^34.0.0", | ||
"typescript": "^3.0.1", | ||
@@ -96,3 +95,3 @@ "webpack": "^4.17.1" | ||
"types": "build/es2015/module.d.ts", | ||
"version": "2.1.1" | ||
"version": "2.1.2" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
323
23326
- Removedbabel-runtime@^6.26.0
- Removedbabel-runtime@6.26.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedregenerator-runtime@0.11.1(transitive)