atomic-fns 0.1.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.
Files changed (52) hide show
  1. package/.editorconfig +12 -0
  2. package/.eslintignore +1 -0
  3. package/.eslintrc.json +34 -0
  4. package/.prettierrc +19 -0
  5. package/LICENSE +201 -0
  6. package/README.md +1 -0
  7. package/build/collections.d.ts +204 -0
  8. package/build/collections.js +545 -0
  9. package/build/datetime.d.ts +1 -0
  10. package/build/datetime.js +1 -0
  11. package/build/functools.d.ts +36 -0
  12. package/build/functools.js +79 -0
  13. package/build/globals.d.ts +175 -0
  14. package/build/globals.js +332 -0
  15. package/build/index.d.ts +7 -0
  16. package/build/index.js +7 -0
  17. package/build/itertools.d.ts +16 -0
  18. package/build/itertools.js +102 -0
  19. package/build/math.d.ts +16 -0
  20. package/build/math.js +67 -0
  21. package/build/objects.d.ts +0 -0
  22. package/build/objects.js +1 -0
  23. package/build/operators.d.ts +29 -0
  24. package/build/operators.js +128 -0
  25. package/build/random.d.ts +5 -0
  26. package/build/random.js +28 -0
  27. package/build/strings.d.ts +0 -0
  28. package/build/strings.js +1 -0
  29. package/build/util.d.ts +0 -0
  30. package/build/util.js +1 -0
  31. package/jasmine.json +14 -0
  32. package/package.json +71 -0
  33. package/src/collections.ts +614 -0
  34. package/src/datetime.ts +1 -0
  35. package/src/functools.ts +89 -0
  36. package/src/globals.d.ts +5 -0
  37. package/src/globals.ts +377 -0
  38. package/src/index.ts +7 -0
  39. package/src/itertools.ts +113 -0
  40. package/src/math.ts +73 -0
  41. package/src/objects.ts +0 -0
  42. package/src/operators.ts +126 -0
  43. package/src/random.ts +29 -0
  44. package/src/strings.ts +0 -0
  45. package/src/util.ts +0 -0
  46. package/tests/collections.spec.ts +199 -0
  47. package/tests/globals.spec.ts +232 -0
  48. package/tests/itertools.spec.ts +87 -0
  49. package/tests/operators.spec.ts +62 -0
  50. package/tsconfig.json +103 -0
  51. package/tsconfig.node.json +18 -0
  52. package/tsconfig.xo.json +9 -0
package/build/math.js ADDED
@@ -0,0 +1,67 @@
1
+ import { call, isFunc } from './globals.js';
2
+ import { comp } from './operators.js';
3
+ export function abs(x) {
4
+ const op = call(x, 'abs');
5
+ if (op != null)
6
+ return op;
7
+ return Math.abs(x);
8
+ }
9
+ /**
10
+ * Returns a tuple like (x / y, x % y)
11
+ * @export
12
+ * @param {number} x
13
+ * @param {number} y
14
+ * @return {number[]}
15
+ */
16
+ export function divmod(x, y) {
17
+ return [Math.floor(x / y), x % y];
18
+ }
19
+ export function log2(x) {
20
+ if (x > 0) {
21
+ return Math.log(x) * 1.442695;
22
+ }
23
+ return Number.NaN;
24
+ }
25
+ export function logBase(x, y) {
26
+ if (x > 0 && y > 0) {
27
+ return Math.log(y) / Math.log(x);
28
+ }
29
+ return Number.NaN;
30
+ }
31
+ /** First argument can be a comparer func */
32
+ export function min(args) {
33
+ // Uses `le` by default
34
+ let cmp = comp;
35
+ if (isFunc(args[0])) {
36
+ cmp = args.shift();
37
+ }
38
+ let res = args[0];
39
+ for (const x of args) {
40
+ if (cmp(x, res) === -1) {
41
+ res = x;
42
+ }
43
+ }
44
+ return res;
45
+ }
46
+ /** First argument can be a comparer func */
47
+ export function max(args) {
48
+ // Uses ge by default
49
+ let cmp = comp;
50
+ if (isFunc(args[0])) {
51
+ cmp = args.shift();
52
+ }
53
+ let res = args[0];
54
+ for (const x of args) {
55
+ if (cmp(x, res) === 1) {
56
+ res = x;
57
+ }
58
+ }
59
+ return res;
60
+ }
61
+ export function sum(args) {
62
+ let total = 0;
63
+ for (const x of args) {
64
+ total += x;
65
+ }
66
+ return total;
67
+ }
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,29 @@
1
+ export declare const id: (x: any) => any;
2
+ export declare const bool: (x: any) => boolean;
3
+ export declare const not: (x: any) => boolean;
4
+ export declare const isinstance: (x: any, y: any) => boolean;
5
+ export declare const all: (arr: any[], fn?: (x: any) => boolean) => boolean;
6
+ export declare const any: (arr: any[], fn?: (x: any) => boolean) => boolean;
7
+ export declare const and: (arr: any[], fn?: (x: any) => boolean) => boolean;
8
+ export declare const or: (arr: any[], fn?: (x: any) => boolean) => boolean;
9
+ export declare function comp(x: any, y: any): any;
10
+ export declare function eq(x: any, y: any): any;
11
+ export declare function shallowEqual(x: any, y: any): any;
12
+ export declare function lt(x: any, y: any): any;
13
+ export declare function le(x: any, y: any): any;
14
+ export declare function gt(x: any, y: any): any;
15
+ export declare function ge(x: any, y: any): any;
16
+ export declare function add(x: any, y: any): any;
17
+ export declare function sub(x: any, y: any): any;
18
+ export declare function mult(x: any, y: any): any;
19
+ export declare function div(x: any, y: any): any;
20
+ export declare function mod(x: any, y: any): any;
21
+ /**
22
+ * Calculates `x` to the power of `y`. If `x` implements a custom `pow` operator
23
+ * it will return `x.pow(y)`.
24
+ * @export
25
+ * @param {*} x
26
+ * @param {*} y
27
+ * @return `x` raised to the power `y`
28
+ */
29
+ export declare function pow(x: any, y: any): any;
@@ -0,0 +1,128 @@
1
+ import { call, isNumber, isObject } from './globals.js';
2
+ export const id = (x) => x;
3
+ export const bool = (x) => !!x;
4
+ export const not = (x) => !x;
5
+ export const isinstance = (x, y) => x instanceof y;
6
+ export const all = (arr, fn = bool) => arr.every(fn);
7
+ export const any = (arr, fn = bool) => arr.some(fn);
8
+ export const and = all;
9
+ export const or = any;
10
+ export function comp(x, y) {
11
+ const op = call(x, 'compare', y);
12
+ if (isNumber(op))
13
+ return op;
14
+ if (eq(x, y))
15
+ return 0;
16
+ if (le(x, y))
17
+ return -1;
18
+ if (ge(x, y))
19
+ return 1;
20
+ return 0;
21
+ }
22
+ export function eq(x, y) {
23
+ const op = call(x, 'eq', y);
24
+ if (op != null)
25
+ return op;
26
+ return x === y;
27
+ }
28
+ export function shallowEqual(x, y) {
29
+ if (!x || !y)
30
+ return x === y;
31
+ if (Array.isArray(x)) {
32
+ for (let i = 0; i < x.length; i++) {
33
+ if (!eq(x[i], y[i]))
34
+ return false;
35
+ }
36
+ return true;
37
+ }
38
+ if (isObject(x)) {
39
+ if (eq(x, y))
40
+ return true;
41
+ for (const key in x) {
42
+ if (!eq(x[key], y[key]))
43
+ return false;
44
+ }
45
+ return true;
46
+ }
47
+ return eq(x, y);
48
+ }
49
+ export function lt(x, y) {
50
+ const op = call(x, 'lt', y);
51
+ if (op != null)
52
+ return op;
53
+ return x < y;
54
+ }
55
+ export function le(x, y) {
56
+ let op = call(x, 'le', y);
57
+ if (op != null)
58
+ return op;
59
+ op = call(x, 'eq', y);
60
+ if (op === true)
61
+ return op;
62
+ op = call(x, 'lt', y);
63
+ if (op != null)
64
+ return op;
65
+ return x <= y;
66
+ }
67
+ export function gt(x, y) {
68
+ const op = call(x, 'gt', y);
69
+ if (op != null)
70
+ return op;
71
+ return x > y;
72
+ }
73
+ export function ge(x, y) {
74
+ let op = call(x, 'ge', y);
75
+ if (op != null)
76
+ return op;
77
+ op = call(x, 'eq', y);
78
+ if (op === true)
79
+ return op;
80
+ op = call(x, 'gt', y);
81
+ if (op != null)
82
+ return op;
83
+ return x >= y;
84
+ }
85
+ export function add(x, y) {
86
+ const op = call(x, 'add', y);
87
+ if (op != null)
88
+ return op;
89
+ return x + y;
90
+ }
91
+ export function sub(x, y) {
92
+ const op = call(x, 'sub', y);
93
+ if (op != null)
94
+ return op;
95
+ return x - y;
96
+ }
97
+ export function mult(x, y) {
98
+ const op = call(x, 'mult', y);
99
+ if (op != null)
100
+ return op;
101
+ return x * y;
102
+ }
103
+ export function div(x, y) {
104
+ const op = call(x, 'div', y);
105
+ if (op != null)
106
+ return op;
107
+ return x / y;
108
+ }
109
+ export function mod(x, y) {
110
+ const op = call(x, 'mod', y);
111
+ if (op != null)
112
+ return op;
113
+ return x % y;
114
+ }
115
+ /**
116
+ * Calculates `x` to the power of `y`. If `x` implements a custom `pow` operator
117
+ * it will return `x.pow(y)`.
118
+ * @export
119
+ * @param {*} x
120
+ * @param {*} y
121
+ * @return `x` raised to the power `y`
122
+ */
123
+ export function pow(x, y) {
124
+ const op = call(x, 'pow', y);
125
+ if (op != null)
126
+ return op;
127
+ return x ** y;
128
+ }
@@ -0,0 +1,5 @@
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;
@@ -0,0 +1,28 @@
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
+ }
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
File without changes
package/build/util.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";
package/jasmine.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "reporters": [
3
+ {
4
+ "name": "jasmine-spec-reporter#SpecReporter",
5
+ "options": {
6
+ "displayStacktrace": "all"
7
+ }
8
+ }
9
+ ],
10
+ "spec_dir": "tests",
11
+ "spec_files": [
12
+ "**/*spec.ts"
13
+ ]
14
+ }
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "atomic-fns",
3
+ "description": "A general-purpose and forward looking toolbox for evergreen JS code standards.",
4
+ "version": "0.1.0",
5
+ "main": "build/index.js",
6
+ "type": "module",
7
+ "devDependencies": {
8
+ "@types/jasmine": "^4.3.0",
9
+ "@typescript-eslint/eslint-plugin": "^5.39.0",
10
+ "eslint": "^8.25.0",
11
+ "eslint-config-prettier": "^8.5.0",
12
+ "eslint-config-standard-with-typescript": "^23.0.0",
13
+ "eslint-plugin-import": "^2.26.0",
14
+ "eslint-plugin-n": "^15.3.0",
15
+ "eslint-plugin-prettier": "^4.2.1",
16
+ "eslint-plugin-promise": "^6.0.1",
17
+ "husky": "^8.0.1",
18
+ "jasmine": "^4.4.0",
19
+ "jasmine-spec-reporter": "^7.0.0",
20
+ "lint-staged": "^13.0.3",
21
+ "prettier": "^2.7.1",
22
+ "pretty-quick": "^3.1.3",
23
+ "ts-node": "^10.4.1",
24
+ "typescript": "^4.8.4"
25
+ },
26
+ "scripts": {
27
+ "postinstall": "npm run tsc",
28
+ "test": "NODE_OPTIONS=--loader=ts-node/esm jasmine --config=jasmine.json",
29
+ "tsc": "tsc -p ./tsconfig.xo.json --declaration",
30
+ "lint": "eslint src/**",
31
+ "lint:fix": "eslint src/** --fix",
32
+ "format": "prettier --write src/**"
33
+ },
34
+ "lint-staged": {
35
+ "*.ts": "eslint"
36
+ },
37
+ "husky": {
38
+ "hooks": {
39
+ "precommit": "npm test && lint-staged && pretty-quick --staged"
40
+ }
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/adrian2x/es-next.git"
45
+ },
46
+ "keywords": [
47
+ "es-next",
48
+ "esnext",
49
+ "ecmascript",
50
+ "@esnext",
51
+ "@es-next",
52
+ "es6",
53
+ "es7",
54
+ "es8",
55
+ "es2016",
56
+ "es2017",
57
+ "es2019",
58
+ "es2020",
59
+ "modules",
60
+ "stdlib",
61
+ "core-js",
62
+ "util",
63
+ "fp"
64
+ ],
65
+ "author": "Adrian Cruz <adrian2x>",
66
+ "license": "Apache-2.0",
67
+ "bugs": {
68
+ "url": "https://github.com/adrian2x/es-next/issues"
69
+ },
70
+ "homepage": "https://github.com/adrian2x/es-next#readme"
71
+ }