purus 0.8.0 → 0.9.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.
@@ -0,0 +1,29 @@
1
+ // Purus stdlib: math — Math alias with lowercase constants
2
+ //
3
+ // math = JS Math + lowercase aliases for uppercase constants (PI → pi, etc.)
4
+ // All Math.* methods (abs, sin, floor, ...) are available as-is.
5
+
6
+ exports.spread = "Math";
7
+
8
+ exports.mod = {
9
+ ...Math,
10
+ pi: Math.PI,
11
+ e: Math.E,
12
+ ln2: Math.LN2,
13
+ ln10: Math.LN10,
14
+ log2e: Math.LOG2E,
15
+ log10e: Math.LOG10E,
16
+ sqrt2: Math.SQRT2,
17
+ sqrt1_2: Math.SQRT1_2,
18
+ };
19
+
20
+ exports.constants = {
21
+ pi: "PI",
22
+ e: "E",
23
+ ln2: "LN2",
24
+ ln10: "LN10",
25
+ log2e: "LOG2E",
26
+ log10e: "LOG10E",
27
+ sqrt2: "SQRT2",
28
+ sqrt1_2: "SQRT1_2",
29
+ };
@@ -0,0 +1,51 @@
1
+ // Purus stdlib: p-number — Number utility functions
2
+ //
3
+ // test: node -e "const n = require('./stdlib/p-number'); console.log(n.mod.isfinite(42))"
4
+
5
+ exports.mod = {
6
+ isfinite(val) {
7
+ return Number.isFinite(val);
8
+ },
9
+ isinteger(val) {
10
+ return Number.isInteger(val);
11
+ },
12
+ isnan(val) {
13
+ return Number.isNaN(val);
14
+ },
15
+ issafe(val) {
16
+ return Number.isSafeInteger(val);
17
+ },
18
+ parsefloat(str) {
19
+ return Number.parseFloat(str);
20
+ },
21
+ parseint(str, radix) {
22
+ return Number.parseInt(str, radix || 10);
23
+ },
24
+ tofixed(num, digits) {
25
+ return Number(num).toFixed(digits || 0);
26
+ },
27
+ toprecision(num, digits) {
28
+ return Number(num).toPrecision(digits);
29
+ },
30
+ toexponential(num, digits) {
31
+ return Number(num).toExponential(digits);
32
+ },
33
+ tostring(num, radix) {
34
+ return Number(num).toString(radix || 10);
35
+ },
36
+ clamp(num, min, max) {
37
+ return Math.min(Math.max(num, min), max);
38
+ },
39
+ };
40
+
41
+ exports.spread = "Number";
42
+
43
+ exports.constants = {
44
+ maxsafe: "MAX_SAFE_INTEGER",
45
+ minsafe: "MIN_SAFE_INTEGER",
46
+ epsilon: "EPSILON",
47
+ maxvalue: "MAX_VALUE",
48
+ minvalue: "MIN_VALUE",
49
+ posinf: "POSITIVE_INFINITY",
50
+ neginf: "NEGATIVE_INFINITY",
51
+ };
@@ -0,0 +1,69 @@
1
+ // Purus stdlib: p-object — Object utility functions
2
+ //
3
+ // test: node -e "const o = require('./stdlib/p-object'); console.log(o.mod.keys({a:1,b:2}))"
4
+
5
+ exports.mod = {
6
+ keys(obj) {
7
+ return Object.keys(obj);
8
+ },
9
+ values(obj) {
10
+ return Object.values(obj);
11
+ },
12
+ entries(obj) {
13
+ return Object.entries(obj);
14
+ },
15
+ fromentries(arr) {
16
+ return Object.fromEntries(arr);
17
+ },
18
+ assign(target, ...sources) {
19
+ return Object.assign(target, ...sources);
20
+ },
21
+ freeze(obj) {
22
+ return Object.freeze(obj);
23
+ },
24
+ seal(obj) {
25
+ return Object.seal(obj);
26
+ },
27
+ isfrozen(obj) {
28
+ return Object.isFrozen(obj);
29
+ },
30
+ issealed(obj) {
31
+ return Object.isSealed(obj);
32
+ },
33
+ hasown(obj, key) {
34
+ return Object.hasOwn(obj, key);
35
+ },
36
+ create(proto, props) {
37
+ return Object.create(proto, props);
38
+ },
39
+ is(a, b) {
40
+ return Object.is(a, b);
41
+ },
42
+ len(obj) {
43
+ return Object.keys(obj).length;
44
+ },
45
+ merge(...objs) {
46
+ return Object.assign({}, ...objs);
47
+ },
48
+ clone(obj) {
49
+ return structuredClone(obj);
50
+ },
51
+ pick(obj, keys) {
52
+ const result = {};
53
+ for (const k of keys) {
54
+ if (k in obj) result[k] = obj[k];
55
+ }
56
+ return result;
57
+ },
58
+ omit(obj, keys) {
59
+ const set = new Set(keys);
60
+ const result = {};
61
+ for (const k of Object.keys(obj)) {
62
+ if (!set.has(k)) result[k] = obj[k];
63
+ }
64
+ return result;
65
+ },
66
+ };
67
+
68
+ exports.spread = null;
69
+ exports.constants = {};
@@ -0,0 +1,45 @@
1
+ // Purus stdlib: p-promise — Promise utility functions
2
+ //
3
+ // test: node -e "const p = require('./stdlib/p-promise'); p.mod.resolve(42).then(v => console.log(v))"
4
+
5
+ exports.mod = {
6
+ resolve(value) {
7
+ return Promise.resolve(value);
8
+ },
9
+ reject(reason) {
10
+ return Promise.reject(reason);
11
+ },
12
+ all(promises) {
13
+ return Promise.all(promises);
14
+ },
15
+ allsettled(promises) {
16
+ return Promise.allSettled(promises);
17
+ },
18
+ race(promises) {
19
+ return Promise.race(promises);
20
+ },
21
+ any(promises) {
22
+ return Promise.any(promises);
23
+ },
24
+ create(executor) {
25
+ return new Promise(executor);
26
+ },
27
+ delay(ms) {
28
+ return new Promise(function (resolve) {
29
+ setTimeout(resolve, ms);
30
+ });
31
+ },
32
+ timeout(promise, ms) {
33
+ return Promise.race([
34
+ promise,
35
+ new Promise(function (_, reject) {
36
+ setTimeout(function () {
37
+ reject(new Error("Promise timed out"));
38
+ }, ms);
39
+ }),
40
+ ]);
41
+ },
42
+ };
43
+
44
+ exports.spread = null;
45
+ exports.constants = {};
@@ -0,0 +1,218 @@
1
+ // Purus stdlib: random
2
+ //
3
+ // test: node -e "const r = require('./stdlib/random'); console.log(r.mod.randint(1, 10))"
4
+
5
+ exports.mod = {
6
+ // --- core ---
7
+ random() {
8
+ return Math.random();
9
+ },
10
+ randint(a, b) {
11
+ return Math.floor(Math.random() * (b - a + 1)) + a;
12
+ },
13
+ randrange(...args) {
14
+ let start = 0,
15
+ stop,
16
+ step = 1;
17
+ if (args.length === 1) {
18
+ stop = args[0];
19
+ } else if (args.length === 2) {
20
+ start = args[0];
21
+ stop = args[1];
22
+ } else {
23
+ start = args[0];
24
+ stop = args[1];
25
+ step = args[2];
26
+ }
27
+ const n = Math.ceil((stop - start) / step);
28
+ return start + step * Math.floor(Math.random() * n);
29
+ },
30
+ randbool(p) {
31
+ if (p === undefined) p = 0.5;
32
+ return Math.random() < p;
33
+ },
34
+ getrandbits(k) {
35
+ if (k <= 0) return 0;
36
+ if (k <= 32) return (Math.random() * (1 << k)) >>> 0;
37
+ let result = 0;
38
+ let remaining = k;
39
+ while (remaining > 0) {
40
+ const bits = Math.min(remaining, 32);
41
+ result = result * (1 << bits) + ((Math.random() * (1 << bits)) >>> 0);
42
+ remaining -= bits;
43
+ }
44
+ return result;
45
+ },
46
+ randbytes(n) {
47
+ const bytes = new Uint8Array(n);
48
+ for (let i = 0; i < n; i++) {
49
+ bytes[i] = (Math.random() * 256) >>> 0;
50
+ }
51
+ return Array.from(bytes);
52
+ },
53
+ normalvariate(mu, sigma) {
54
+ return this.gauss(mu, sigma);
55
+ },
56
+
57
+ // --- real-valued distributions ---
58
+ uniform(a, b) {
59
+ return a + (b - a) * Math.random();
60
+ },
61
+ triangular(lo, hi, mode) {
62
+ if (lo === undefined) lo = 0;
63
+ if (hi === undefined) hi = 1;
64
+ if (mode === undefined) mode = (lo + hi) / 2;
65
+ const u = Math.random(),
66
+ c = (mode - lo) / (hi - lo);
67
+ return u < c
68
+ ? lo + Math.sqrt(u * (hi - lo) * (mode - lo))
69
+ : hi - Math.sqrt((1 - u) * (hi - lo) * (hi - mode));
70
+ },
71
+ gauss(mu, sigma) {
72
+ if (mu === undefined) mu = 0;
73
+ if (sigma === undefined) sigma = 1;
74
+ let u, v, s;
75
+ do {
76
+ u = Math.random() * 2 - 1;
77
+ v = Math.random() * 2 - 1;
78
+ s = u * u + v * v;
79
+ } while (s >= 1 || s === 0);
80
+ return mu + sigma * u * Math.sqrt((-2 * Math.log(s)) / s);
81
+ },
82
+ expovariate(lambd) {
83
+ return -Math.log(1 - Math.random()) / lambd;
84
+ },
85
+ gammavariate(alpha, beta) {
86
+ if (alpha > 1) {
87
+ const d = alpha - 1 / 3,
88
+ c = 1 / Math.sqrt(9 * d);
89
+ let x, v;
90
+ do {
91
+ do {
92
+ x = this.gauss(0, 1);
93
+ v = 1 + c * x;
94
+ } while (v <= 0);
95
+ v = v * v * v;
96
+ } while (
97
+ Math.log(Math.random()) >=
98
+ 0.5 * x * x + d - d * v + d * Math.log(v)
99
+ );
100
+ return d * v * beta;
101
+ }
102
+ if (alpha === 1) return -Math.log(1 - Math.random()) * beta;
103
+ return (
104
+ this.gammavariate(alpha + 1, beta) * Math.pow(Math.random(), 1 / alpha)
105
+ );
106
+ },
107
+ betavariate(alpha, beta) {
108
+ const y = this.gammavariate(alpha, 1);
109
+ return y === 0 ? 0 : y / (y + this.gammavariate(beta, 1));
110
+ },
111
+ lognormvariate(mu, sigma) {
112
+ return Math.exp(this.gauss(mu, sigma));
113
+ },
114
+ vonmisesvariate(mu, kappa) {
115
+ if (kappa <= 1e-6) return 2 * Math.PI * Math.random();
116
+ const a = 1 + Math.sqrt(1 + 4 * kappa * kappa),
117
+ b = (a - Math.sqrt(2 * a)) / (2 * kappa),
118
+ r = (1 + b * b) / (2 * b);
119
+ let f;
120
+ do {
121
+ const u1 = Math.random(),
122
+ z = Math.cos(Math.PI * u1),
123
+ d = z / (r + z),
124
+ u2 = Math.random();
125
+ if (u2 < 1 - d * d || u2 <= (1 - d) * Math.exp(d)) {
126
+ f = (1 / r + z) / (1 + r * z);
127
+ break;
128
+ }
129
+ } while (true);
130
+ return (
131
+ (((Math.random() > 0.5 ? mu + Math.acos(f) : mu - Math.acos(f)) %
132
+ (2 * Math.PI)) +
133
+ 2 * Math.PI) %
134
+ (2 * Math.PI)
135
+ );
136
+ },
137
+ paretovariate(alpha) {
138
+ return Math.pow(1 - Math.random(), -1 / alpha);
139
+ },
140
+ weibullvariate(alpha, beta) {
141
+ return alpha * Math.pow(-Math.log(1 - Math.random()), 1 / beta);
142
+ },
143
+
144
+ // --- sequence ---
145
+ choice(arr) {
146
+ return arr[Math.floor(Math.random() * arr.length)];
147
+ },
148
+ choices(arr, k) {
149
+ return Array.from(
150
+ { length: k },
151
+ () => arr[Math.floor(Math.random() * arr.length)],
152
+ );
153
+ },
154
+ wchoices(arr, weights, k) {
155
+ const cum = [];
156
+ let s = 0;
157
+ for (const w of weights) {
158
+ s += w;
159
+ cum.push(s);
160
+ }
161
+ return Array.from({ length: k }, () => {
162
+ const r = Math.random() * s;
163
+ let lo = 0,
164
+ hi = cum.length - 1;
165
+ while (lo < hi) {
166
+ const mid = (lo + hi) >> 1;
167
+ cum[mid] < r ? (lo = mid + 1) : (hi = mid);
168
+ }
169
+ return arr[lo];
170
+ });
171
+ },
172
+ shuffle(arr) {
173
+ const a = [...arr];
174
+ for (let i = a.length - 1; i > 0; i--) {
175
+ const j = Math.floor(Math.random() * (i + 1));
176
+ [a[i], a[j]] = [a[j], a[i]];
177
+ }
178
+ return a;
179
+ },
180
+ sample(arr, k) {
181
+ const a = [...arr];
182
+ for (let i = a.length - 1; i > 0; i--) {
183
+ const j = Math.floor(Math.random() * (i + 1));
184
+ [a[i], a[j]] = [a[j], a[i]];
185
+ }
186
+ return a.slice(0, k);
187
+ },
188
+
189
+ // --- discrete ---
190
+ binomial(n, p) {
191
+ let x = 0;
192
+ for (let i = 0; i < n; i++) {
193
+ if (Math.random() < p) x++;
194
+ }
195
+ return x;
196
+ },
197
+ poisson(lambda) {
198
+ const L = Math.exp(-lambda);
199
+ let k = 0,
200
+ p = 1;
201
+ do {
202
+ k++;
203
+ p *= Math.random();
204
+ } while (p > L);
205
+ return k - 1;
206
+ },
207
+ geometric(p) {
208
+ return Math.floor(Math.log(1 - Math.random()) / Math.log(1 - p)) + 1;
209
+ },
210
+
211
+ // --- utility ---
212
+ clamp(val, lo, hi) {
213
+ return Math.min(Math.max(val, lo), hi);
214
+ },
215
+ lerp(a, b, t) {
216
+ return a + (b - a) * t;
217
+ },
218
+ };
@@ -0,0 +1,50 @@
1
+ // Purus stdlib: p-regexp — Regular expression utilities
2
+ //
3
+ // test: node -e "const r = require('./stdlib/p-regexp'); console.log(r.mod.test(/\d+/, '42'))"
4
+
5
+ exports.mod = {
6
+ create(pattern, flags) {
7
+ return new RegExp(pattern, flags || "");
8
+ },
9
+ test(re, str) {
10
+ return re instanceof RegExp ? re.test(str) : new RegExp(re).test(str);
11
+ },
12
+ match(re, str) {
13
+ return str.match(re);
14
+ },
15
+ matchall(re, str) {
16
+ const r = re instanceof RegExp ? re : new RegExp(re, "g");
17
+ if (!r.flags.includes("g")) {
18
+ return [...str.matchAll(new RegExp(r.source, r.flags + "g"))];
19
+ }
20
+ return [...str.matchAll(r)];
21
+ },
22
+ exec(re, str) {
23
+ return re.exec(str);
24
+ },
25
+ replace(re, str, replacement) {
26
+ return str.replace(re, replacement);
27
+ },
28
+ replaceall(re, str, replacement) {
29
+ const r = re instanceof RegExp ? re : new RegExp(re, "g");
30
+ if (!r.flags.includes("g")) {
31
+ return str.replaceAll(new RegExp(r.source, r.flags + "g"), replacement);
32
+ }
33
+ return str.replaceAll(r, replacement);
34
+ },
35
+ split(re, str) {
36
+ return str.split(re);
37
+ },
38
+ source(re) {
39
+ return re.source;
40
+ },
41
+ flags(re) {
42
+ return re.flags;
43
+ },
44
+ escape(str) {
45
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
46
+ },
47
+ };
48
+
49
+ exports.spread = null;
50
+ exports.constants = {};
@@ -0,0 +1,61 @@
1
+ // Purus stdlib: p-set — Set creation and operations
2
+ //
3
+ // test: node -e "const s = require('./stdlib/p-set'); console.log(s.mod.from([1,2,3,2,1]))"
4
+
5
+ exports.mod = {
6
+ create(...items) {
7
+ return new Set(items);
8
+ },
9
+ from(iterable) {
10
+ return new Set(iterable);
11
+ },
12
+ add(set, value) {
13
+ return new Set(set).add(value);
14
+ },
15
+ delete(set, value) {
16
+ const s = new Set(set);
17
+ s.delete(value);
18
+ return s;
19
+ },
20
+ has(set, value) {
21
+ return set.has(value);
22
+ },
23
+ size(set) {
24
+ return set.size;
25
+ },
26
+ values(set) {
27
+ return [...set];
28
+ },
29
+ union(a, b) {
30
+ return a.union(b);
31
+ },
32
+ intersection(a, b) {
33
+ return a.intersection(b);
34
+ },
35
+ difference(a, b) {
36
+ return a.difference(b);
37
+ },
38
+ symmetricdifference(a, b) {
39
+ return a.symmetricDifference(b);
40
+ },
41
+ issubset(a, b) {
42
+ return a.isSubsetOf(b);
43
+ },
44
+ issuperset(a, b) {
45
+ return a.isSupersetOf(b);
46
+ },
47
+ isdisjoint(a, b) {
48
+ return a.isDisjointFrom(b);
49
+ },
50
+ clear(set) {
51
+ const s = new Set(set);
52
+ s.clear();
53
+ return s;
54
+ },
55
+ toarray(set) {
56
+ return [...set];
57
+ },
58
+ };
59
+
60
+ exports.spread = null;
61
+ exports.constants = {};
@@ -0,0 +1,103 @@
1
+ // Purus stdlib: string — string utility functions
2
+ //
3
+ // test: node -e "const s = require('./stdlib/string'); console.log(s.mod.reverse('hello'))"
4
+
5
+ exports.mod = {
6
+ // --- query ---
7
+ len(str) {
8
+ return str.length;
9
+ },
10
+ contains(str, sub) {
11
+ return str.includes(sub);
12
+ },
13
+ startswith(str, prefix) {
14
+ return str.startsWith(prefix);
15
+ },
16
+ endswith(str, suffix) {
17
+ return str.endsWith(suffix);
18
+ },
19
+ indexof(str, sub) {
20
+ return str.indexOf(sub);
21
+ },
22
+ count(str, sub) {
23
+ let c = 0,
24
+ i = 0;
25
+ while ((i = str.indexOf(sub, i)) !== -1) {
26
+ c++;
27
+ i += sub.length;
28
+ }
29
+ return c;
30
+ },
31
+
32
+ // --- transform ---
33
+ upper(str) {
34
+ return str.toUpperCase();
35
+ },
36
+ lower(str) {
37
+ return str.toLowerCase();
38
+ },
39
+ capitalize(str) {
40
+ return str.charAt(0).toUpperCase() + str.slice(1);
41
+ },
42
+ title(str) {
43
+ return str.replace(/\b\w/g, (c) => c.toUpperCase());
44
+ },
45
+ trim(str) {
46
+ return str.trim();
47
+ },
48
+ trimstart(str) {
49
+ return str.trimStart();
50
+ },
51
+ trimend(str) {
52
+ return str.trimEnd();
53
+ },
54
+ reverse(str) {
55
+ return [...str].reverse().join("");
56
+ },
57
+ repeat(str, n) {
58
+ return str.repeat(n);
59
+ },
60
+ replace(str, old, rep) {
61
+ return str.replaceAll(old, rep);
62
+ },
63
+ replacefirst(str, old, rep) {
64
+ return str.replace(old, rep);
65
+ },
66
+ padstart(str, len, fill) {
67
+ return str.padStart(len, fill || " ");
68
+ },
69
+ padend(str, len, fill) {
70
+ return str.padEnd(len, fill || " ");
71
+ },
72
+
73
+ // --- split / join ---
74
+ split(str, sep) {
75
+ return str.split(sep);
76
+ },
77
+ lines(str) {
78
+ return str.split(/\r?\n/);
79
+ },
80
+ words(str) {
81
+ return str.split(/\s+/).filter((w) => w.length > 0);
82
+ },
83
+ join(arr, sep) {
84
+ return arr.join(sep);
85
+ },
86
+ chars(str) {
87
+ return [...str];
88
+ },
89
+
90
+ // --- slice ---
91
+ slice(str, start, end) {
92
+ return str.slice(start, end);
93
+ },
94
+ charat(str, i) {
95
+ return str.charAt(i);
96
+ },
97
+ codeat(str, i) {
98
+ return str.codePointAt(i);
99
+ },
100
+ fromcode(code) {
101
+ return String.fromCodePoint(code);
102
+ },
103
+ };