atomic-fns 0.2.0 → 0.2.3

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.
Files changed (64) hide show
  1. package/.esdoc.json +19 -1
  2. package/.eslintrc.json +1 -0
  3. package/README.md +8 -9
  4. package/package.json +9 -9
  5. package/src/collections/index.d.ts +658 -0
  6. package/src/collections/index.js +1068 -0
  7. package/src/collections/index.ts +1162 -0
  8. package/src/datetime/index.d.ts +6 -0
  9. package/src/datetime/index.js +6 -0
  10. package/src/datetime/index.ts +7 -0
  11. package/src/{functools.d.ts → functools/index.d.ts} +10 -5
  12. package/src/{functools.js → functools/index.js} +9 -4
  13. package/src/{functools.ts → functools/index.ts} +11 -5
  14. package/src/globals/index.d.ts +291 -0
  15. package/src/globals/index.js +391 -0
  16. package/src/globals/index.ts +456 -0
  17. package/src/index.d.ts +8 -7
  18. package/src/index.js +8 -7
  19. package/src/index.ts +8 -7
  20. package/src/itertools/index.d.ts +254 -0
  21. package/src/itertools/index.js +485 -0
  22. package/src/itertools/index.ts +511 -0
  23. package/src/math/index.d.ts +21 -0
  24. package/src/math/index.js +39 -0
  25. package/src/math/index.ts +44 -0
  26. package/src/{operators.d.ts → operators/index.d.ts} +8 -6
  27. package/src/{operators.js → operators/index.js} +69 -26
  28. package/src/{operators.ts → operators/index.ts} +77 -28
  29. package/src/random/index.d.ts +42 -0
  30. package/src/random/index.js +65 -0
  31. package/src/random/index.ts +66 -0
  32. package/tests/collections.spec.ts +119 -30
  33. package/tests/globals.spec.ts +91 -49
  34. package/tests/itertools.spec.ts +207 -14
  35. package/tests/operators.spec.ts +7 -29
  36. package/tests/random.spec.ts +46 -0
  37. package/tsconfig.json +19 -3
  38. package/src/collections.d.ts +0 -224
  39. package/src/collections.js +0 -570
  40. package/src/collections.ts +0 -637
  41. package/src/datetime.d.ts +0 -1
  42. package/src/datetime.js +0 -1
  43. package/src/datetime.ts +0 -1
  44. package/src/globals.d.ts +0 -175
  45. package/src/globals.js +0 -329
  46. package/src/globals.ts +0 -373
  47. package/src/itertools.d.ts +0 -16
  48. package/src/itertools.js +0 -101
  49. package/src/itertools.ts +0 -112
  50. package/src/math.d.ts +0 -16
  51. package/src/math.js +0 -67
  52. package/src/math.ts +0 -73
  53. package/src/objects.d.ts +0 -0
  54. package/src/objects.js +0 -1
  55. package/src/objects.ts +0 -0
  56. package/src/random.d.ts +0 -22
  57. package/src/random.js +0 -48
  58. package/src/random.ts +0 -49
  59. package/src/strings.d.ts +0 -0
  60. package/src/strings.js +0 -1
  61. package/src/strings.ts +0 -0
  62. package/src/util.d.ts +0 -0
  63. package/src/util.js +0 -1
  64. package/src/util.ts +0 -0
package/src/math.ts DELETED
@@ -1,73 +0,0 @@
1
- import { call, isFunc } from './globals.js'
2
- import { comp } from './operators.js'
3
-
4
- export function abs(x: number) {
5
- const op = call(x, 'abs')
6
- if (op != null) return op
7
- return Math.abs(x)
8
- }
9
-
10
- /**
11
- * Returns a tuple like (x / y, x % y)
12
- *
13
- * @param {number} x
14
- * @param {number} y
15
- * @return {number[]}
16
- */
17
- export function divmod(x: number, y: number) {
18
- return [Math.floor(x / y), x % y]
19
- }
20
-
21
- export function log2(x) {
22
- if (x > 0) {
23
- return Math.log(x) * 1.442695
24
- }
25
- return Number.NaN
26
- }
27
-
28
- export function logBase(x, y) {
29
- if (x > 0 && y > 0) {
30
- return Math.log(y) / Math.log(x)
31
- }
32
- return Number.NaN
33
- }
34
-
35
- /** First argument can be a comparer func */
36
- export function min(args) {
37
- // Uses `le` by default
38
- let cmp = comp
39
- if (isFunc(args[0])) {
40
- cmp = args.shift()
41
- }
42
- let res = args[0]
43
- for (const x of args) {
44
- if (cmp(x, res) === -1) {
45
- res = x
46
- }
47
- }
48
- return res
49
- }
50
-
51
- /** First argument can be a comparer func */
52
- export function max(args) {
53
- // Uses ge by default
54
- let cmp = comp
55
- if (isFunc(args[0])) {
56
- cmp = args.shift()
57
- }
58
- let res = args[0]
59
- for (const x of args) {
60
- if (cmp(x, res) === 1) {
61
- res = x
62
- }
63
- }
64
- return res
65
- }
66
-
67
- export function sum(args) {
68
- let total = 0
69
- for (const x of args) {
70
- total += x
71
- }
72
- return total
73
- }
package/src/objects.d.ts DELETED
File without changes
package/src/objects.js DELETED
@@ -1 +0,0 @@
1
- "use strict";
package/src/objects.ts DELETED
File without changes
package/src/random.d.ts DELETED
@@ -1,22 +0,0 @@
1
- /** Returns a random float between a [min, max) value (max exclusive). */
2
- export declare const random: (min?: number, max?: number) => number;
3
- /** Returns a random integer between a [min, max] value (inclusive). */
4
- export declare const randomInt: (min?: number, max?: number) => number;
5
- /** Returns the item at some random index in a sequence. */
6
- export declare const choice: (arr: any[]) => any;
7
- /**
8
- * Returns a random sample of size k from a list of items.
9
- *
10
- * @export
11
- * @param {any[]} arr
12
- * @param {number} k
13
- * @return
14
- */
15
- export declare function sample(arr: any[], k: number): any[] | undefined;
16
- /**
17
- * Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
18
- *
19
- * @param {*} arr
20
- * @return
21
- */
22
- export declare function shuffle(arr: any): any;
package/src/random.js DELETED
@@ -1,48 +0,0 @@
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 DELETED
@@ -1,49 +0,0 @@
1
- import { isObject, round } from './globals'
2
-
3
- /** Returns a random float between a [min, max) value (max exclusive). */
4
- export const random = (min = 0, max = 1) => Math.random() * (max - min) + min
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))
8
-
9
- /** Returns the item at some random index in a sequence. */
10
- export const choice = (arr: any[]) => {
11
- if (!arr || !arr.length) return
12
- return arr[randomInt(0, arr.length - 1)]
13
- }
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
- */
23
- export function sample(arr: any[], k: number) {
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
- }
29
- return result
30
- }
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
- */
38
- export function shuffle(arr) {
39
- if (!arr || !arr.length) return
40
- if (isObject(arr)) arr = Object.values(arr)
41
- const n = arr.length - 1
42
- for (let i = n; i > 0; i--) {
43
- const j = randomInt(0, i)
44
- const temp = arr[j]
45
- arr[j] = arr[i]
46
- arr[i] = temp
47
- }
48
- return arr
49
- }
package/src/strings.d.ts DELETED
File without changes
package/src/strings.js DELETED
@@ -1 +0,0 @@
1
- "use strict";
package/src/strings.ts DELETED
File without changes
package/src/util.d.ts DELETED
File without changes
package/src/util.js DELETED
@@ -1 +0,0 @@
1
- "use strict";
package/src/util.ts DELETED
File without changes