atomic-fns 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.esdoc.json +9 -0
- package/README.md +14 -12
- package/package.json +22 -12
- package/{build → src}/collections.d.ts +30 -10
- package/{build → src}/collections.js +46 -21
- package/src/collections.ts +46 -23
- package/{build → src}/datetime.d.ts +0 -0
- package/{build → src}/datetime.js +0 -0
- package/{build → src}/functools.d.ts +3 -3
- package/{build → src}/functools.js +3 -3
- package/src/functools.ts +3 -3
- package/src/globals.d.ts +174 -4
- package/{build → src}/globals.js +18 -21
- package/src/globals.ts +16 -20
- package/{build → src}/index.d.ts +0 -0
- package/{build → src}/index.js +0 -0
- package/{build → src}/itertools.d.ts +0 -0
- package/{build → src}/itertools.js +4 -5
- package/src/itertools.ts +4 -5
- package/{build → src}/math.d.ts +1 -1
- package/{build → src}/math.js +1 -1
- package/src/math.ts +1 -1
- package/{build → src}/objects.d.ts +0 -0
- package/{build → src}/objects.js +0 -0
- package/{build → src}/operators.d.ts +1 -1
- package/{build → src}/operators.js +1 -1
- package/src/operators.ts +1 -1
- package/src/random.d.ts +22 -0
- package/src/random.js +48 -0
- package/src/random.ts +27 -7
- package/{build → src}/strings.d.ts +0 -0
- package/{build → src}/strings.js +0 -0
- package/{build → src}/util.d.ts +0 -0
- package/{build → src}/util.js +0 -0
- package/tests/globals.spec.ts +4 -2
- package/tsconfig.json +8 -2
- package/build/globals.d.ts +0 -175
- package/build/random.d.ts +0 -5
- package/build/random.js +0 -28
package/src/random.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { isObject } from './globals';
|
|
2
|
+
/** Returns a random float between a [min, max) value (max exclusive). */
|
|
3
|
+
export const random = (min = 0, max = 1) => Math.random() * (max - min) + min;
|
|
4
|
+
/** Returns a random integer between a [min, max] value (inclusive). */
|
|
5
|
+
export const randomInt = (min = 0, max = 1) => Math.round(random(min, max));
|
|
6
|
+
/** Returns the item at some random index in a sequence. */
|
|
7
|
+
export const choice = (arr) => {
|
|
8
|
+
if (!arr || !arr.length)
|
|
9
|
+
return;
|
|
10
|
+
return arr[randomInt(0, arr.length - 1)];
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Returns a random sample of size k from a list of items.
|
|
14
|
+
*
|
|
15
|
+
* @export
|
|
16
|
+
* @param {any[]} arr
|
|
17
|
+
* @param {number} k
|
|
18
|
+
* @return
|
|
19
|
+
*/
|
|
20
|
+
export function sample(arr, k) {
|
|
21
|
+
if (!arr || !arr.length || arr.length < k)
|
|
22
|
+
return;
|
|
23
|
+
const result = [];
|
|
24
|
+
for (let i = 0; i < k; i++) {
|
|
25
|
+
result.push(choice(arr));
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
|
|
31
|
+
*
|
|
32
|
+
* @param {*} arr
|
|
33
|
+
* @return
|
|
34
|
+
*/
|
|
35
|
+
export function shuffle(arr) {
|
|
36
|
+
if (!arr || !arr.length)
|
|
37
|
+
return;
|
|
38
|
+
if (isObject(arr))
|
|
39
|
+
arr = Object.values(arr);
|
|
40
|
+
const n = arr.length - 1;
|
|
41
|
+
for (let i = n; i > 0; i--) {
|
|
42
|
+
const j = randomInt(0, i);
|
|
43
|
+
const temp = arr[j];
|
|
44
|
+
arr[j] = arr[i];
|
|
45
|
+
arr[i] = temp;
|
|
46
|
+
}
|
|
47
|
+
return arr;
|
|
48
|
+
}
|
package/src/random.ts
CHANGED
|
@@ -1,23 +1,42 @@
|
|
|
1
1
|
import { isObject, round } from './globals'
|
|
2
2
|
|
|
3
|
+
/** Returns a random float between a [min, max) value (max exclusive). */
|
|
3
4
|
export const random = (min = 0, max = 1) => Math.random() * (max - min) + min
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
/** Returns a random integer between a [min, max] value (inclusive). */
|
|
7
|
+
export const randomInt = (min = 0, max = 1) => Math.round(random(min, max))
|
|
6
8
|
|
|
9
|
+
/** Returns the item at some random index in a sequence. */
|
|
7
10
|
export const choice = (arr: any[]) => {
|
|
8
|
-
if (!arr
|
|
9
|
-
|
|
10
|
-
return arr[index]
|
|
11
|
+
if (!arr || !arr.length) return
|
|
12
|
+
return arr[randomInt(0, arr.length - 1)]
|
|
11
13
|
}
|
|
12
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Returns a random sample of size k from a list of items.
|
|
17
|
+
*
|
|
18
|
+
* @export
|
|
19
|
+
* @param {any[]} arr
|
|
20
|
+
* @param {number} k
|
|
21
|
+
* @return
|
|
22
|
+
*/
|
|
13
23
|
export function sample(arr: any[], k: number) {
|
|
14
|
-
if (!arr
|
|
15
|
-
const result:
|
|
16
|
-
|
|
24
|
+
if (!arr || !arr.length || arr.length < k) return
|
|
25
|
+
const result: typeof arr = []
|
|
26
|
+
for (let i = 0; i < k; i++) {
|
|
27
|
+
result.push(choice(arr))
|
|
28
|
+
}
|
|
17
29
|
return result
|
|
18
30
|
}
|
|
19
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
|
|
34
|
+
*
|
|
35
|
+
* @param {*} arr
|
|
36
|
+
* @return
|
|
37
|
+
*/
|
|
20
38
|
export function shuffle(arr) {
|
|
39
|
+
if (!arr || !arr.length) return
|
|
21
40
|
if (isObject(arr)) arr = Object.values(arr)
|
|
22
41
|
const n = arr.length - 1
|
|
23
42
|
for (let i = n; i > 0; i--) {
|
|
@@ -26,4 +45,5 @@ export function shuffle(arr) {
|
|
|
26
45
|
arr[j] = arr[i]
|
|
27
46
|
arr[i] = temp
|
|
28
47
|
}
|
|
48
|
+
return arr
|
|
29
49
|
}
|
|
File without changes
|
package/{build → src}/strings.js
RENAMED
|
File without changes
|
package/{build → src}/util.d.ts
RENAMED
|
File without changes
|
package/{build → src}/util.js
RENAMED
|
File without changes
|
package/tests/globals.spec.ts
CHANGED
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
values,
|
|
27
27
|
round,
|
|
28
28
|
enumerate,
|
|
29
|
+
isEmpty,
|
|
29
30
|
isGenerator,
|
|
30
31
|
hash,
|
|
31
32
|
hashCode,
|
|
@@ -36,8 +37,7 @@ import {
|
|
|
36
37
|
list,
|
|
37
38
|
next,
|
|
38
39
|
get
|
|
39
|
-
} from '../
|
|
40
|
-
import { isEmpty } from '../src/globals.js'
|
|
40
|
+
} from '../src/globals.js'
|
|
41
41
|
|
|
42
42
|
function cb() {}
|
|
43
43
|
|
|
@@ -109,6 +109,8 @@ describe('globals', () => {
|
|
|
109
109
|
it('str', () => {
|
|
110
110
|
assert(str(undefined) === '')
|
|
111
111
|
assert(str(null) === '')
|
|
112
|
+
assert(str(true) === 'true')
|
|
113
|
+
assert(str(false) === 'false')
|
|
112
114
|
assert(str(NaN) === 'NaN')
|
|
113
115
|
assert(str(42) === '42')
|
|
114
116
|
assert(str([]) === '')
|
package/tsconfig.json
CHANGED
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
48
48
|
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
49
49
|
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
50
|
-
"outDir": "./build", /* Specify an output folder for all emitted files. */
|
|
50
|
+
// "outDir": "./build", /* Specify an output folder for all emitted files. */
|
|
51
51
|
// "removeComments": true, /* Disable emitting comments. */
|
|
52
52
|
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
53
53
|
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
@@ -99,5 +99,11 @@
|
|
|
99
99
|
{
|
|
100
100
|
"path": "./tsconfig.node.json"
|
|
101
101
|
}
|
|
102
|
-
]
|
|
102
|
+
],
|
|
103
|
+
"typedocOptions": {
|
|
104
|
+
"entryPoints": [
|
|
105
|
+
"src/index.ts"
|
|
106
|
+
],
|
|
107
|
+
"out": "docs"
|
|
108
|
+
}
|
|
103
109
|
}
|
package/build/globals.d.ts
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
export declare type Comp = (x: any, y: any) => number;
|
|
2
|
-
export declare type Function = (...args: any[]) => any;
|
|
3
|
-
export declare type Iteratee = (value: any, key?: any, arr?: any) => any;
|
|
4
|
-
export declare const True: () => boolean;
|
|
5
|
-
export declare const False: () => boolean;
|
|
6
|
-
/**
|
|
7
|
-
* A custom error class that inherits from the Error object.
|
|
8
|
-
*
|
|
9
|
-
* @export
|
|
10
|
-
* @class CustomError
|
|
11
|
-
* @extends {Error}
|
|
12
|
-
*/
|
|
13
|
-
export declare class CustomError extends Error {
|
|
14
|
-
/**
|
|
15
|
-
* Returns a new CustomError with the specified message.
|
|
16
|
-
* @param message
|
|
17
|
-
* @constructor
|
|
18
|
-
*/
|
|
19
|
-
constructor(message?: any);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Custom error to signal an invalid index access.
|
|
23
|
-
* @extends {CustomError}
|
|
24
|
-
*/
|
|
25
|
-
export declare class IndexError extends CustomError {
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Raised when a system function returns an I/O failure such as "file not found"
|
|
29
|
-
* or "disk full".
|
|
30
|
-
* @extends {CustomError}
|
|
31
|
-
*/
|
|
32
|
-
export declare class IOError extends CustomError {
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Custom error to signal an invalid key access.
|
|
36
|
-
* @extends {CustomError}
|
|
37
|
-
*/
|
|
38
|
-
export declare class KeyError extends CustomError {
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Custom error to signal an invalid operation.
|
|
42
|
-
* @extends {CustomError}
|
|
43
|
-
*/
|
|
44
|
-
export declare class NotImplementedError extends CustomError {
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Custom error to signal an invalid value.
|
|
48
|
-
* @extends {CustomError}
|
|
49
|
-
*/
|
|
50
|
-
export declare class ValueError extends CustomError {
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Raised when the second argument of a division or modulo operation is zero.
|
|
54
|
-
* @extends {CustomError}
|
|
55
|
-
*/
|
|
56
|
-
export declare class ZeroDivisionError extends CustomError {
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Returns the true type of any value with correct detection for null, Array,
|
|
60
|
-
* Object, Promise, Symbol, and NaN.
|
|
61
|
-
*
|
|
62
|
-
* @export
|
|
63
|
-
* @param {*} value
|
|
64
|
-
* @return
|
|
65
|
-
*/
|
|
66
|
-
export declare function type(value: any): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" | "promise" | "null" | "NaN";
|
|
67
|
-
/** Same as calling toString() */
|
|
68
|
-
export declare const str: (x: any) => string;
|
|
69
|
-
/** Check if value is a boolean type */
|
|
70
|
-
export declare const isBool: (x: any) => boolean;
|
|
71
|
-
/** Check if value is an object type */
|
|
72
|
-
export declare const isObject: (x: any) => boolean;
|
|
73
|
-
/** Check if value is a string type */
|
|
74
|
-
export declare const isString: (x: any) => boolean;
|
|
75
|
-
/** Check if value is an Array type */
|
|
76
|
-
export declare const isArray: (x: any) => boolean;
|
|
77
|
-
/** Check if value is Array-like type.
|
|
78
|
-
* A value is considered array-like if it's not a function and has a
|
|
79
|
-
* `.length` number property.
|
|
80
|
-
*/
|
|
81
|
-
export declare const isArrayLike: (x: any) => boolean;
|
|
82
|
-
/** Check if value is a function type */
|
|
83
|
-
export declare const isFunc: (x: any) => boolean;
|
|
84
|
-
/** Check if value is a number type */
|
|
85
|
-
export declare const isNumber: (x: any) => boolean;
|
|
86
|
-
/** Check if value is a Bigint type */
|
|
87
|
-
export declare const isBigint: (x: any) => boolean;
|
|
88
|
-
/** Check if value is NaN based on `Number.isNaN` */
|
|
89
|
-
export declare const isNaN: (x: any) => boolean;
|
|
90
|
-
/** Check if value is a Promise type */
|
|
91
|
-
export declare const isPromise: (x: any) => boolean;
|
|
92
|
-
/** Check if value is an async function type */
|
|
93
|
-
export declare const isAsync: (x: any) => boolean;
|
|
94
|
-
/** Check if value is a generator function type */
|
|
95
|
-
export declare const isGenerator: (x: any) => boolean;
|
|
96
|
-
/** Check if value is null or undefined */
|
|
97
|
-
export declare const isNull: (x: any) => boolean;
|
|
98
|
-
/** Check if value is not null or undefined */
|
|
99
|
-
export declare const notNull: (x: any) => boolean;
|
|
100
|
-
export declare function len(x: any): any;
|
|
101
|
-
/** Returns true for objects without length or falsey values. */
|
|
102
|
-
export declare const isEmpty: (x: any) => boolean;
|
|
103
|
-
/** Check if value is a Symbol type */
|
|
104
|
-
export declare const isSymbol: (x: any) => boolean;
|
|
105
|
-
/**
|
|
106
|
-
* Generates a unique ID using random numbers.
|
|
107
|
-
* If prefix is given, the ID is appended to it
|
|
108
|
-
*/
|
|
109
|
-
export declare const uniqueId: (pre?: string) => any;
|
|
110
|
-
/**
|
|
111
|
-
* Checks if `obj.key` is a function, and calls it with any `args`.
|
|
112
|
-
*
|
|
113
|
-
* @export
|
|
114
|
-
* @param {*} obj
|
|
115
|
-
* @param {PropertyKey} key
|
|
116
|
-
* @param {...any[]} args
|
|
117
|
-
* @return `obj.key(...args)`
|
|
118
|
-
*/
|
|
119
|
-
export declare function call(obj: any, key: PropertyKey, ...args: any[]): any;
|
|
120
|
-
/** Convert number to unicode character */
|
|
121
|
-
export declare const chr: (x: number) => string;
|
|
122
|
-
/** Convert character to Unicode code point */
|
|
123
|
-
export declare const ord: (x: string) => number;
|
|
124
|
-
/** `Object.keys(x)` */
|
|
125
|
-
export declare const keys: (x: any) => string[];
|
|
126
|
-
/** `Object.values(x)` */
|
|
127
|
-
export declare const values: (x: any) => unknown[];
|
|
128
|
-
/**
|
|
129
|
-
* Round `x` to the number of digits after the decimal point. If `digits` is
|
|
130
|
-
* omitted, it returns the nearest integer to x.
|
|
131
|
-
* @export
|
|
132
|
-
* @param {number} x
|
|
133
|
-
* @param {number} [digits=0]
|
|
134
|
-
*/
|
|
135
|
-
export declare function round(x: number, digits?: number): number;
|
|
136
|
-
export declare const floor: (x: number) => number;
|
|
137
|
-
export declare const ceil: (x: number) => number;
|
|
138
|
-
/**
|
|
139
|
-
* Yields elements like [index, item] from an iterable.
|
|
140
|
-
*
|
|
141
|
-
* @export
|
|
142
|
-
* @template T
|
|
143
|
-
* @param {Iterable<T>} iterable
|
|
144
|
-
*/
|
|
145
|
-
export declare function enumerate<T = unknown>(iterable: Iterable<T>): Generator<(number | T)[], void, unknown>;
|
|
146
|
-
export declare const has: (obj: any, attr: PropertyKey) => any;
|
|
147
|
-
/**
|
|
148
|
-
* Check if the attribute is present in the object, or return optional default.
|
|
149
|
-
*
|
|
150
|
-
* @export
|
|
151
|
-
* @param {*} obj
|
|
152
|
-
* @param {PropertyKey} key
|
|
153
|
-
* @param {*} [value=undefined]
|
|
154
|
-
* @return The property value or default value
|
|
155
|
-
*/
|
|
156
|
-
export declare function get(obj: any, key: PropertyKey | PropertyKey[], value?: any): any;
|
|
157
|
-
export declare function del(x: any, attr: any): any;
|
|
158
|
-
export declare function set(self: any, attr: PropertyKey, value: any, writable?: boolean, enumerable?: boolean): any;
|
|
159
|
-
export declare const HASH_KEY: unique symbol;
|
|
160
|
-
export declare function hash(obj: any): any;
|
|
161
|
-
/**
|
|
162
|
-
* A hash value generator for strings
|
|
163
|
-
* @export
|
|
164
|
-
* @param {string} str
|
|
165
|
-
* @return {number}
|
|
166
|
-
* @see http://isthe.com/chongo/tech/comp/fnv/
|
|
167
|
-
*/
|
|
168
|
-
export declare function hashCode(str: string): number;
|
|
169
|
-
export declare const bin: (n: number) => string;
|
|
170
|
-
export declare const hex: (n: number) => string;
|
|
171
|
-
export declare const oct: (n: number) => string;
|
|
172
|
-
export declare const float: (x: any) => number;
|
|
173
|
-
export declare const int: (x: any, base?: number) => number;
|
|
174
|
-
export declare const list: (arr?: any) => unknown[];
|
|
175
|
-
export declare const next: (iter: Iterator<unknown>) => any;
|
package/build/random.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare const random: (min?: number, max?: number) => number;
|
|
2
|
-
export declare const randomInt: (min?: number, max?: number) => number;
|
|
3
|
-
export declare const choice: (arr: any[]) => any;
|
|
4
|
-
export declare function sample(arr: any[], k: number): any[] | undefined;
|
|
5
|
-
export declare function shuffle(arr: any): void;
|
package/build/random.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { isObject, round } from './globals';
|
|
2
|
-
export const random = (min = 0, max = 1) => Math.random() * (max - min) + min;
|
|
3
|
-
export const randomInt = (min = 0, max = 1) => round(random(min, max), 0);
|
|
4
|
-
export const choice = (arr) => {
|
|
5
|
-
if (!arr?.length)
|
|
6
|
-
return;
|
|
7
|
-
const index = randomInt(0, arr.length - 1);
|
|
8
|
-
return arr[index];
|
|
9
|
-
};
|
|
10
|
-
export function sample(arr, k) {
|
|
11
|
-
if (!arr?.length)
|
|
12
|
-
return;
|
|
13
|
-
const result = [];
|
|
14
|
-
while (k--)
|
|
15
|
-
result.push(choice(arr));
|
|
16
|
-
return result;
|
|
17
|
-
}
|
|
18
|
-
export function shuffle(arr) {
|
|
19
|
-
if (isObject(arr))
|
|
20
|
-
arr = Object.values(arr);
|
|
21
|
-
const n = arr.length - 1;
|
|
22
|
-
for (let i = n; i > 0; i--) {
|
|
23
|
-
const j = randomInt(0, i);
|
|
24
|
-
const temp = arr[j];
|
|
25
|
-
arr[j] = arr[i];
|
|
26
|
-
arr[i] = temp;
|
|
27
|
-
}
|
|
28
|
-
}
|