first commit

This commit is contained in:
Myk
2025-07-31 23:47:20 +03:00
commit 2186b278a0
5149 changed files with 537218 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
import { TAddUniqueNumberFactory } from '../types';
export declare const createAddUniqueNumber: TAddUniqueNumberFactory;
//# sourceMappingURL=add-unique-number.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"add-unique-number.d.ts","sourceRoot":"","sources":["../../../src/factories/add-unique-number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAC;AAEnD,eAAO,MAAM,qBAAqB,EAAE,uBAQnC,CAAC"}

View File

@@ -0,0 +1,8 @@
export const createAddUniqueNumber = (generateUniqueNumber) => {
return (set) => {
const number = generateUniqueNumber(set);
set.add(number);
return number;
};
};
//# sourceMappingURL=add-unique-number.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"add-unique-number.js","sourceRoot":"","sources":["../../../src/factories/add-unique-number.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,qBAAqB,GAA4B,CAAC,oBAAoB,EAAE,EAAE;IACnF,OAAO,CAAC,GAAG,EAAE,EAAE;QACX,MAAM,MAAM,GAAG,oBAAoB,CAAC,GAAG,CAAC,CAAC;QAEzC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAEhB,OAAO,MAAM,CAAC;IAClB,CAAC,CAAC;AACN,CAAC,CAAC"}

View File

@@ -0,0 +1,3 @@
import { TCacheFactory } from '../types';
export declare const createCache: TCacheFactory;
//# sourceMappingURL=cache.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../../src/factories/cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,eAAO,MAAM,WAAW,EAAE,aAMzB,CAAC"}

View File

@@ -0,0 +1,7 @@
export const createCache = (lastNumberWeakMap) => {
return (collection, nextNumber) => {
lastNumberWeakMap.set(collection, nextNumber);
return nextNumber;
};
};
//# sourceMappingURL=cache.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../../src/factories/cache.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,WAAW,GAAkB,CAAC,iBAAiB,EAAE,EAAE;IAC5D,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,EAAE;QAC9B,iBAAiB,CAAC,GAAG,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;QAE9C,OAAO,UAAU,CAAC;IACtB,CAAC,CAAC;AACN,CAAC,CAAC"}

View File

@@ -0,0 +1,3 @@
import { TGenerateUniqueNumberFactory } from '../types';
export declare const createGenerateUniqueNumber: TGenerateUniqueNumberFactory;
//# sourceMappingURL=generate-unique-number.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"generate-unique-number.d.ts","sourceRoot":"","sources":["../../../src/factories/generate-unique-number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AAUxD,eAAO,MAAM,0BAA0B,EAAE,4BA4CxC,CAAC"}

View File

@@ -0,0 +1,46 @@
/*
* The value of the constant Number.MAX_SAFE_INTEGER equals (2 ** 53 - 1) but it
* is fairly new.
*/
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER === undefined ? 9007199254740991 : Number.MAX_SAFE_INTEGER;
const TWO_TO_THE_POWER_OF_TWENTY_NINE = 536870912;
const TWO_TO_THE_POWER_OF_THIRTY = TWO_TO_THE_POWER_OF_TWENTY_NINE * 2;
export const createGenerateUniqueNumber = (cache, lastNumberWeakMap) => {
return (collection) => {
const lastNumber = lastNumberWeakMap.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 < TWO_TO_THE_POWER_OF_THIRTY ? lastNumber + 1 : 0;
if (!collection.has(nextNumber)) {
return cache(collection, nextNumber);
}
/*
* If there are less than half of 2 ** 30 numbers stored in the collection,
* the chance to generate a new random number in the range from 0 to 2 ** 30
* 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 < TWO_TO_THE_POWER_OF_TWENTY_NINE) {
while (collection.has(nextNumber)) {
nextNumber = Math.floor(Math.random() * TWO_TO_THE_POWER_OF_THIRTY);
}
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=generate-unique-number.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"generate-unique-number.js","sourceRoot":"","sources":["../../../src/factories/generate-unique-number.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,gBAAgB,GAAG,MAAM,CAAC,gBAAgB,KAAK,SAAS,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,MAAM,CAAC,gBAAgB,CAAC;AAC5G,MAAM,+BAA+B,GAAG,SAAS,CAAC;AAClD,MAAM,0BAA0B,GAAG,+BAA+B,GAAG,CAAC,CAAC;AAEvE,MAAM,CAAC,MAAM,0BAA0B,GAAiC,CAAC,KAAK,EAAE,iBAAiB,EAAE,EAAE;IACjG,OAAO,CAAC,UAAU,EAAE,EAAE;QAClB,MAAM,UAAU,GAAG,iBAAiB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAErD;;;;;;;WAOG;QACH,IAAI,UAAU,GAAG,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,U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