@rimbu/base 0.11.4 → 1.0.0-alpha.2

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 (70) hide show
  1. package/{src/arr.ts → dist/bun/arr.mts} +1 -1
  2. package/dist/bun/index.mts +6 -0
  3. package/dist/bun/internal.mts +1 -0
  4. package/dist/cjs/arr.cjs +177 -0
  5. package/dist/cjs/entry.cjs +37 -0
  6. package/dist/cjs/index.cjs +247 -0
  7. package/dist/cjs/internal.cjs +32 -0
  8. package/dist/cjs/plain-object.cjs +37 -0
  9. package/dist/cjs/rimbu-error.cjs +75 -0
  10. package/dist/cjs/token.cjs +30 -0
  11. package/dist/{module/arr.js → esm/arr.mjs} +1 -1
  12. package/dist/esm/arr.mjs.map +1 -0
  13. package/dist/{module/entry.js → esm/entry.mjs} +1 -1
  14. package/dist/esm/entry.mjs.map +1 -0
  15. package/dist/esm/index.mjs +6 -0
  16. package/dist/esm/index.mjs.map +1 -0
  17. package/dist/esm/internal.mjs +2 -0
  18. package/dist/esm/internal.mjs.map +1 -0
  19. package/dist/{module/plain-object.js → esm/plain-object.mjs} +1 -1
  20. package/dist/esm/plain-object.mjs.map +1 -0
  21. package/dist/{module/rimbu-error.js → esm/rimbu-error.mjs} +1 -1
  22. package/dist/esm/rimbu-error.mjs.map +1 -0
  23. package/dist/{module/token.js → esm/token.mjs} +1 -1
  24. package/dist/esm/token.mjs.map +1 -0
  25. package/dist/types/{arr.d.ts → arr.d.mts} +1 -1
  26. package/dist/types/index.d.mts +5 -0
  27. package/dist/types/internal.d.mts +1 -0
  28. package/package.json +27 -21
  29. package/src/arr.mts +195 -0
  30. package/src/entry.mts +9 -0
  31. package/src/index.mts +6 -0
  32. package/src/internal.mts +1 -0
  33. package/src/plain-object.mts +95 -0
  34. package/src/rimbu-error.mts +39 -0
  35. package/src/token.mts +2 -0
  36. package/dist/main/arr.js +0 -169
  37. package/dist/main/arr.js.map +0 -1
  38. package/dist/main/entry.js +0 -14
  39. package/dist/main/entry.js.map +0 -1
  40. package/dist/main/index.js +0 -10
  41. package/dist/main/index.js.map +0 -1
  42. package/dist/main/internal.js +0 -5
  43. package/dist/main/internal.js.map +0 -1
  44. package/dist/main/plain-object.js +0 -27
  45. package/dist/main/plain-object.js.map +0 -1
  46. package/dist/main/rimbu-error.js +0 -54
  47. package/dist/main/rimbu-error.js.map +0 -1
  48. package/dist/main/token.js +0 -5
  49. package/dist/main/token.js.map +0 -1
  50. package/dist/module/arr.js.map +0 -1
  51. package/dist/module/entry.js.map +0 -1
  52. package/dist/module/index.js +0 -6
  53. package/dist/module/index.js.map +0 -1
  54. package/dist/module/internal.js +0 -2
  55. package/dist/module/internal.js.map +0 -1
  56. package/dist/module/plain-object.js.map +0 -1
  57. package/dist/module/rimbu-error.js.map +0 -1
  58. package/dist/module/token.js.map +0 -1
  59. package/dist/types/index.d.ts +0 -5
  60. package/dist/types/internal.d.ts +0 -1
  61. package/src/index.ts +0 -6
  62. package/src/internal.ts +0 -1
  63. /package/{src/entry.ts → dist/bun/entry.mts} +0 -0
  64. /package/{src/plain-object.ts → dist/bun/plain-object.mts} +0 -0
  65. /package/{src/rimbu-error.ts → dist/bun/rimbu-error.mts} +0 -0
  66. /package/{src/token.ts → dist/bun/token.mts} +0 -0
  67. /package/dist/types/{entry.d.ts → entry.d.mts} +0 -0
  68. /package/dist/types/{plain-object.d.ts → plain-object.d.mts} +0 -0
  69. /package/dist/types/{rimbu-error.d.ts → rimbu-error.d.mts} +0 -0
  70. /package/dist/types/{token.d.ts → token.d.mts} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/index.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,WAAW,CAAC;AACjC,OAAO,KAAK,KAAK,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;AAChD,cAAc,oBAAoB,CAAC;AAEnC,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './token.mjs';
2
+ //# sourceMappingURL=internal.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal.mjs","sourceRoot":"","sources":["../../src/internal.mts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -19,4 +19,4 @@ export function isPlainObj(obj) {
19
19
  export function isIterable(obj) {
20
20
  return obj !== null && typeof obj === 'object' && Symbol.iterator in obj;
21
21
  }
22
- //# sourceMappingURL=plain-object.js.map
22
+ //# sourceMappingURL=plain-object.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plain-object.mjs","sourceRoot":"","sources":["../../src/plain-object.mts"],"names":[],"mappings":"AAuEA;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,GAAQ;IACjC,OAAO,CACL,OAAO,GAAG,KAAK,QAAQ;QACvB,IAAI,KAAK,GAAG;QACZ,CAAC,GAAG,CAAC,WAAW,KAAK,MAAM,IAAI,CAAC,CAAC,GAAG,CAAC,WAAW,YAAY,QAAQ,CAAC,CAAC;QACtE,CAAC,CAAC,MAAM,CAAC,QAAQ,IAAI,GAAG,CAAC;QACzB,CAAC,CAAC,MAAM,CAAC,aAAa,IAAI,GAAG,CAAC,CAC/B,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,GAAQ;IACjC,OAAO,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,IAAI,GAAG,CAAC;AAC3E,CAAC"}
@@ -28,4 +28,4 @@ export function throwInvalidStateError() {
28
28
  export function throwInvalidUsageError(msg) {
29
29
  throw new InvalidUsageError(msg);
30
30
  }
31
- //# sourceMappingURL=rimbu-error.js.map
31
+ //# sourceMappingURL=rimbu-error.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rimbu-error.mjs","sourceRoot":"","sources":["../../src/rimbu-error.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC,MAAM,OAAO,mCAAoC,SAAQ,OAAO,CAAC,WAAW;IAC1E;QACE,KAAK,CAAC,+CAA+C,CAAC,CAAC;IACzD,CAAC;CACF;AAED,MAAM,OAAO,sCAAuC,SAAQ,OAAO,CAAC,WAAW;IAC7E;QACE,KAAK,CAAC,+DAA+D,CAAC,CAAC;IACzE,CAAC;CACF;AAED,MAAM,OAAO,iBAAkB,SAAQ,OAAO,CAAC,WAAW;IACxD;QACE,KAAK,CACH,4EAA4E,CAC7E,CAAC;IACJ,CAAC;CACF;AAED,MAAM,OAAO,iBAAkB,SAAQ,OAAO,CAAC,WAAW;CAAG;AAE7D,MAAM,UAAU,wCAAwC;IACtD,MAAM,IAAI,mCAAmC,EAAE,CAAC;AAClD,CAAC;AAED,MAAM,UAAU,2CAA2C;IACzD,MAAM,IAAI,sCAAsC,EAAE,CAAC;AACrD,CAAC;AAED,MAAM,UAAU,sBAAsB;IACpC,MAAM,IAAI,iBAAiB,EAAE,CAAC;AAChC,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,GAAW;IAChD,MAAM,IAAI,iBAAiB,CAAC,GAAG,CAAC,CAAC;AACnC,CAAC"}
@@ -1,2 +1,2 @@
1
1
  export const Token = Symbol('Token');
2
- //# sourceMappingURL=token.js.map
2
+ //# sourceMappingURL=token.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token.mjs","sourceRoot":"","sources":["../../src/token.mts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC"}
@@ -1,4 +1,4 @@
1
- import { Update, TraverseState, ArrayNonEmpty } from '@rimbu/common';
1
+ import { Update, TraverseState, type ArrayNonEmpty } from '@rimbu/common';
2
2
  export declare function append<T>(array: readonly T[], value: T): ArrayNonEmpty<T>;
3
3
  export declare function concat<T>(first: readonly T[], second: readonly T[]): readonly T[];
4
4
  export declare function reverse<T>(array: readonly T[], start?: number, end?: number): T[];
@@ -0,0 +1,5 @@
1
+ export * as Arr from './arr.mjs';
2
+ export * as Entry from './entry.mjs';
3
+ export * as RimbuError from './rimbu-error.mjs';
4
+ export * from './plain-object.mjs';
5
+ export * from './internal.mjs';
@@ -0,0 +1 @@
1
+ export * from './token.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rimbu/base",
3
- "version": "0.11.4",
3
+ "version": "1.0.0-alpha.2",
4
4
  "description": "Utilities to implement Rimbu collections",
5
5
  "keywords": [
6
6
  "array",
@@ -26,17 +26,17 @@
26
26
  "url": "https://github.com/rimbu-org/rimbu.git",
27
27
  "directory": "packages/base"
28
28
  },
29
- "source": "./src/index.ts",
30
- "main": "./dist/main/index.js",
31
- "module": "./dist/module/index.js",
32
- "types": "./dist/types/index.d.ts",
29
+ "type": "module",
30
+ "main": "./dist/cjs/index.cjs",
31
+ "module": "./dist/esm/index.mjs",
32
+ "types": "./dist/types/index.d.mts",
33
33
  "exports": {
34
34
  ".": {
35
- "types": "./dist/types/index.d.ts",
36
- "bun": "./src/index.ts",
37
- "import": "./dist/module/index.js",
38
- "require": "./dist/main/index.js",
39
- "default": "./dist/module/index.js"
35
+ "types": "./dist/types/index.d.mts",
36
+ "bun": "./dist/bun/index.mts",
37
+ "import": "./dist/esm/index.mjs",
38
+ "require": "./dist/cjs/index.cjs",
39
+ "default": "./dist/esm/index.mjs"
40
40
  }
41
41
  },
42
42
  "files": [
@@ -45,27 +45,33 @@
45
45
  ],
46
46
  "scripts": {
47
47
  "build": "yarn clean && yarn bundle",
48
- "build:deno": "rimraf deno_dist ../../deno_dist/base && denoify && mv deno_dist ../../deno_dist/base",
49
- "bundle": "yarn bundle:main && yarn bundle:module && yarn bundle:types",
50
- "bundle:main": "tsc --p tsconfig.main.json",
51
- "bundle:module": "tsc --p tsconfig.module.json",
48
+ "build:deno": "yarn bundle:deno-prepare && yarn bundle:deno-convert && yarn bundle:deno-move && yarn bundle:deno-clean",
49
+ "bundle": "yarn bundle:cjs && yarn bundle:esm && yarn bundle:types && yarn bundle:bun",
50
+ "bundle:bun": "node ../../config/bunnify.mjs",
51
+ "bundle:cjs": "tsup src --format cjs --clean -d dist/cjs --loader '.mts=ts'",
52
+ "bundle:deno-prepare": "node ../../config/prepare-denoify.mjs",
53
+ "bundle:deno-convert": "denoify --src _deno_prepare/src",
54
+ "bundle:deno-move": "rimraf ../../deno_dist/base && mv deno_dist ../../deno_dist/base",
55
+ "bundle:deno-clean": "rimraf _deno_prepare",
56
+ "bundle:esm": "tsc --p tsconfig.esm.json",
52
57
  "bundle:types": "tsc --p tsconfig.types.json",
53
58
  "clean": "rimraf dist",
54
59
  "format": "yarn format:base --write",
55
- "format:base": "prettier \"{!CHANGELOG.md}|**/**/*.{ts,tsx,js,json,md}\"",
60
+ "format:base": "prettier \"{!CHANGELOG.md}|**/**/*.{ts,tsx,js,mts,mjs,json,md}\"",
56
61
  "format:check": "yarn format:base --check",
57
62
  "lint": "eslint src",
58
- "test": "jest",
59
- "test:types": "tsd",
63
+ "test": "vitest run",
64
+ "test:cov": "vitest run --coverage",
65
+ "test:watch": "vitest",
66
+ "test:types": "tsd --files test-d",
60
67
  "typecheck": "tsc"
61
68
  },
62
- "sideEffects": false,
63
69
  "dependencies": {
64
- "@rimbu/common": "*",
65
- "tslib": "^2.5.0"
70
+ "@rimbu/common": "^1.0.0-alpha.2",
71
+ "tslib": "^2.5.3"
66
72
  },
67
73
  "publishConfig": {
68
74
  "access": "public"
69
75
  },
70
- "gitHead": "5400f8471bdfc7a227f7defc4a3942bdf3ff1423"
76
+ "gitHead": "5f9e27c3b41028a7ec98c47a94daebce9461d3fb"
71
77
  }
package/src/arr.mts ADDED
@@ -0,0 +1,195 @@
1
+ import { Update, TraverseState, type ArrayNonEmpty } from '@rimbu/common';
2
+
3
+ // Returns a copy of the array with the given value appended
4
+ export function append<T>(array: readonly T[], value: T): ArrayNonEmpty<T> {
5
+ const clone = array.slice();
6
+ clone.push(value);
7
+ return clone as ArrayNonEmpty<T>;
8
+ }
9
+
10
+ // Returns the concatenation of the two arrays, potentially reusing the input array if one of the arrays is empty
11
+ export function concat<T>(
12
+ first: readonly T[],
13
+ second: readonly T[]
14
+ ): readonly T[] {
15
+ if (first.length === 0) return second;
16
+ if (second.length === 0) return first;
17
+ return first.concat(second);
18
+ }
19
+
20
+ // Returns an copy of the array between the start and end indices, with the elements in reversed order.
21
+ export function reverse<T>(
22
+ array: readonly T[],
23
+ start = 0,
24
+ end = array.length - 1
25
+ ): T[] {
26
+ const length = end - start + 1;
27
+ const res = [] as T[];
28
+
29
+ let arrayIndex = start - 1;
30
+ let resIndex = length - 1;
31
+
32
+ while (++arrayIndex <= end) res[resIndex--] = array[arrayIndex];
33
+
34
+ return res;
35
+ }
36
+
37
+ // Performs given function on each element of the array, in reverse order if 'reversed' is true.
38
+ export function forEach<T>(
39
+ array: readonly T[],
40
+ f: (value: T, index: number, halt: () => void) => void,
41
+ state: TraverseState = TraverseState(),
42
+ reversed = false
43
+ ): void {
44
+ if (state.halted) return;
45
+
46
+ const { halt } = state;
47
+
48
+ if (reversed) {
49
+ let i = array.length;
50
+
51
+ while (!state.halted && --i >= 0) {
52
+ f(array[i], state.nextIndex(), halt);
53
+ }
54
+ } else {
55
+ const length = array.length;
56
+ let i = -1;
57
+
58
+ while (!state.halted && ++i < length) {
59
+ f(array[i], state.nextIndex(), halt);
60
+ }
61
+ }
62
+ }
63
+
64
+ // Returns a copy of the array where given function is applied to each element
65
+ export function map<T, R>(
66
+ array: readonly T[],
67
+ f: (value: T, index: number) => R,
68
+ indexOffset = 0
69
+ ): R[] {
70
+ const result: R[] = [];
71
+
72
+ let index = indexOffset;
73
+ let i = -1;
74
+ const length = array.length;
75
+ while (++i < length) {
76
+ result[i] = f(array[i], index++);
77
+ }
78
+ return result;
79
+ }
80
+
81
+ // Returns a copy of the array where given functio is applied to each element in reverse order
82
+ export function reverseMap<T, R>(
83
+ array: readonly T[],
84
+ f: (value: T, index: number) => R,
85
+ indexOffset = 0
86
+ ): R[] {
87
+ const result: R[] = [];
88
+
89
+ let index = indexOffset;
90
+ let arrayIndex = array.length;
91
+ let resultIndex = 0;
92
+ while (--arrayIndex >= 0)
93
+ result[resultIndex++] = f(array[arrayIndex], index++);
94
+
95
+ return result;
96
+ }
97
+
98
+ // Returns a copy of the given array with the given value added at the start
99
+ export function prepend<T>(array: readonly T[], value: T): ArrayNonEmpty<T> {
100
+ const clone = array.slice();
101
+ clone.unshift(value);
102
+ return clone as ArrayNonEmpty<T>;
103
+ }
104
+
105
+ // Returns the last element of the array
106
+ export function last<T>(arr: readonly T[]): T {
107
+ return arr[arr.length - 1];
108
+ }
109
+
110
+ // Returns a copy of the array where the element at given index is replaced by the given updater.
111
+ // If the new element is the same as the old element, the original array is returned
112
+ export function update<T>(
113
+ arr: readonly T[],
114
+ index: number,
115
+ updater: Update<T>
116
+ ): readonly T[] {
117
+ if (index < 0 || index >= arr.length) return arr;
118
+ const curValue = arr[index];
119
+
120
+ const newValue = Update(curValue, updater);
121
+ if (Object.is(newValue, curValue)) return arr;
122
+ const newArr = arr.slice();
123
+ newArr[index] = newValue;
124
+ return newArr;
125
+ }
126
+
127
+ // Returns a copy of the array where the element at given index is replaced by applying given function.
128
+ // If the new element is the same as the old element, the original array is returned
129
+ export function mod<T>(
130
+ arr: readonly T[],
131
+ index: number,
132
+ f: (value: T) => T
133
+ ): readonly T[] {
134
+ if (index < 0 || index >= arr.length) return arr;
135
+
136
+ const curValue = arr[index];
137
+ const newValue = f(curValue);
138
+ if (Object.is(newValue, curValue)) return arr;
139
+ const newArr = arr.slice();
140
+ newArr[index] = newValue;
141
+ return newArr;
142
+ }
143
+
144
+ // Returns a copy of the array where at given index the given value is inserted
145
+ export function insert<T>(arr: readonly T[], index: number, value: T): T[] {
146
+ const clone = arr.slice();
147
+ clone.splice(index, 0, value);
148
+ return clone;
149
+ }
150
+
151
+ // Returns a copy of the array, without its first element
152
+ export function tail<T>(arr: readonly T[]): T[] {
153
+ return arr.slice(1);
154
+ }
155
+
156
+ // Returns a copy of the array, without its last element
157
+ export function init<T>(arr: readonly T[]): T[] {
158
+ return arr.slice(0, arr.length - 1);
159
+ }
160
+
161
+ // Immutable version of the array .splice command, always returns a new array
162
+ export function splice<T>(
163
+ arr: readonly T[],
164
+ start: number,
165
+ deleteCount: number,
166
+ ...items: T[]
167
+ ): T[] {
168
+ const clone = arr.slice();
169
+ clone.splice(start, deleteCount, ...items);
170
+ return clone;
171
+ }
172
+
173
+ // Returns a copy of the array, where its 'sparse' property is kept (sparse = not all indices have a value)
174
+ export function copySparse<T>(arr: readonly T[]): T[] {
175
+ const clone: T[] = [];
176
+ for (const key in arr) {
177
+ clone[key] = arr[key];
178
+ }
179
+ return clone;
180
+ }
181
+
182
+ // Returns a copy of the array with given function applied to each element, where its 'sparse' property is kept
183
+ // (sparse = not all indices have a value)
184
+ export function mapSparse<T, T2>(
185
+ arr: readonly T[],
186
+ f: (value: T, index: number) => T2
187
+ ): T2[] {
188
+ const result: T2[] = Array(arr.length);
189
+
190
+ for (const key in arr) {
191
+ result[key] = f(arr[key], key as any);
192
+ }
193
+
194
+ return result;
195
+ }
package/src/entry.mts ADDED
@@ -0,0 +1,9 @@
1
+ // Returns the first element of a 2-Tuple
2
+ export function first<K, V>(entry: readonly [K, V]): K {
3
+ return entry[0];
4
+ }
5
+
6
+ // Returns the second element of a 2-Tuple
7
+ export function second<K, V>(entry: readonly [K, V]): V {
8
+ return entry[1];
9
+ }
package/src/index.mts ADDED
@@ -0,0 +1,6 @@
1
+ export * as Arr from './arr.mjs';
2
+ export * as Entry from './entry.mjs';
3
+ export * as RimbuError from './rimbu-error.mjs';
4
+ export * from './plain-object.mjs';
5
+
6
+ export * from './internal.mjs';
@@ -0,0 +1 @@
1
+ export * from './token.mjs';
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Matches any type of function
3
+ */
4
+ export type AnyFunc = (...args: any[]) => any;
5
+
6
+ /**
7
+ * Gives true if the given type T is a function, false otherwise.
8
+ * @typeparam T - the input type
9
+ */
10
+ export type IsAnyFunc<T> = AnyFunc extends T ? true : false;
11
+
12
+ /**
13
+ * A predicate type for any record that resolves to true if any of the record
14
+ * properties is a function, false otherwise.
15
+ * This is useful to have a coarse discrimination between pure data objects and class instances.
16
+ * @typeparam T - the input type
17
+ */
18
+ export type IsObjWithoutFunctions<T> = AnyFunc extends T[keyof T]
19
+ ? false
20
+ : true;
21
+
22
+ /**
23
+ * A predicate type that resolves to true if the given type satisfies:
24
+ * - it is an object type (not a primitive)
25
+ * - it is not a function
26
+ * - it is not iterable
27
+ * - it does not have any properties that are functions
28
+ * Otherwise, it resolves to false
29
+ * @typeparam T - the input type
30
+ */
31
+ export type IsPlainObj<T> = T extends
32
+ | null
33
+ | undefined
34
+ | number
35
+ | string
36
+ | boolean
37
+ | bigint
38
+ | symbol
39
+ | AnyFunc
40
+ | Iterable<any>
41
+ | AsyncIterable<any>
42
+ ? false
43
+ : IsObjWithoutFunctions<T>;
44
+
45
+ /**
46
+ * Utility type that will only accept objects that are considered 'plain objects' according
47
+ * to the `IsPlainObj` predicate type.
48
+ * @typeparam T - the value type to test
49
+ */
50
+ export type PlainObj<T> = IsPlainObj<T> extends true ? T : never;
51
+
52
+ /**
53
+ * Utility type that will only return true if the input type T is equal to `any`.
54
+ * @typeparam T - the value type to test
55
+ */
56
+ export type IsAny<T> = 0 extends 1 & T ? true : false;
57
+
58
+ /**
59
+ * Utility type that will only return true if the input type T is a (readonly) array.
60
+ * @typeparm T - the value type to test
61
+ */
62
+ export type IsArray<T> = T extends readonly any[] ? true : false;
63
+
64
+ /**
65
+ * Utility type to exclude any types that are iterable. Useful in cases where
66
+ * plain objects are required as inputs but not arrays.
67
+ */
68
+ export type NotIterable = {
69
+ [Symbol.iterator]?: never;
70
+ };
71
+
72
+ /**
73
+ * Companion function to the `IsRecord<T>` type that checks whether the given object is a pure
74
+ * data object.
75
+ * @param obj - the object to check
76
+ * @returns true if the given object is a pure data object
77
+ * @note does not check whether a record's properties are not functions
78
+ */
79
+ export function isPlainObj(obj: any): obj is object {
80
+ return (
81
+ typeof obj === 'object' &&
82
+ null !== obj &&
83
+ (obj.constructor === Object || !(obj.constructor instanceof Function)) &&
84
+ !(Symbol.iterator in obj) &&
85
+ !(Symbol.asyncIterator in obj)
86
+ );
87
+ }
88
+
89
+ /**
90
+ * Returns true if the given object is Iterable
91
+ * @param obj - the object to check
92
+ */
93
+ export function isIterable(obj: any): obj is Iterable<unknown> {
94
+ return obj !== null && typeof obj === 'object' && Symbol.iterator in obj;
95
+ }
@@ -0,0 +1,39 @@
1
+ import { ErrBase } from '@rimbu/common';
2
+
3
+ export class EmptyCollectionAssumedNonEmptyError extends ErrBase.CustomError {
4
+ constructor() {
5
+ super('empty collection was assumbed to be non-empty');
6
+ }
7
+ }
8
+
9
+ export class ModifiedBuilderWhileLoopingOverItError extends ErrBase.CustomError {
10
+ constructor() {
11
+ super('an attempt was made to modify a builder while looping over it');
12
+ }
13
+ }
14
+
15
+ export class InvalidStateError extends ErrBase.CustomError {
16
+ constructor() {
17
+ super(
18
+ "something happend that shouldn't happen, please consider creating an issue"
19
+ );
20
+ }
21
+ }
22
+
23
+ export class InvalidUsageError extends ErrBase.CustomError {}
24
+
25
+ export function throwEmptyCollectionAssumedNonEmptyError(): never {
26
+ throw new EmptyCollectionAssumedNonEmptyError();
27
+ }
28
+
29
+ export function throwModifiedBuilderWhileLoopingOverItError(): never {
30
+ throw new ModifiedBuilderWhileLoopingOverItError();
31
+ }
32
+
33
+ export function throwInvalidStateError(): never {
34
+ throw new InvalidStateError();
35
+ }
36
+
37
+ export function throwInvalidUsageError(msg: string): never {
38
+ throw new InvalidUsageError(msg);
39
+ }
package/src/token.mts ADDED
@@ -0,0 +1,2 @@
1
+ export const Token = Symbol('Token');
2
+ export type Token = typeof Token;
package/dist/main/arr.js DELETED
@@ -1,169 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapSparse = exports.copySparse = exports.splice = exports.init = exports.tail = exports.insert = exports.mod = exports.update = exports.last = exports.prepend = exports.reverseMap = exports.map = exports.forEach = exports.reverse = exports.concat = exports.append = void 0;
4
- var tslib_1 = require("tslib");
5
- var common_1 = require("@rimbu/common");
6
- // Returns a copy of the array with the given value appended
7
- function append(array, value) {
8
- var clone = array.slice();
9
- clone.push(value);
10
- return clone;
11
- }
12
- exports.append = append;
13
- // Returns the concatenation of the two arrays, potentially reusing the input array if one of the arrays is empty
14
- function concat(first, second) {
15
- if (first.length === 0)
16
- return second;
17
- if (second.length === 0)
18
- return first;
19
- return first.concat(second);
20
- }
21
- exports.concat = concat;
22
- // Returns an copy of the array between the start and end indices, with the elements in reversed order.
23
- function reverse(array, start, end) {
24
- if (start === void 0) { start = 0; }
25
- if (end === void 0) { end = array.length - 1; }
26
- var length = end - start + 1;
27
- var res = [];
28
- var arrayIndex = start - 1;
29
- var resIndex = length - 1;
30
- while (++arrayIndex <= end)
31
- res[resIndex--] = array[arrayIndex];
32
- return res;
33
- }
34
- exports.reverse = reverse;
35
- // Performs given function on each element of the array, in reverse order if 'reversed' is true.
36
- function forEach(array, f, state, reversed) {
37
- if (state === void 0) { state = (0, common_1.TraverseState)(); }
38
- if (reversed === void 0) { reversed = false; }
39
- if (state.halted)
40
- return;
41
- var halt = state.halt;
42
- if (reversed) {
43
- var i = array.length;
44
- while (!state.halted && --i >= 0) {
45
- f(array[i], state.nextIndex(), halt);
46
- }
47
- }
48
- else {
49
- var length_1 = array.length;
50
- var i = -1;
51
- while (!state.halted && ++i < length_1) {
52
- f(array[i], state.nextIndex(), halt);
53
- }
54
- }
55
- }
56
- exports.forEach = forEach;
57
- // Returns a copy of the array where given function is applied to each element
58
- function map(array, f, indexOffset) {
59
- if (indexOffset === void 0) { indexOffset = 0; }
60
- var result = [];
61
- var index = indexOffset;
62
- var i = -1;
63
- var length = array.length;
64
- while (++i < length) {
65
- result[i] = f(array[i], index++);
66
- }
67
- return result;
68
- }
69
- exports.map = map;
70
- // Returns a copy of the array where given functio is applied to each element in reverse order
71
- function reverseMap(array, f, indexOffset) {
72
- if (indexOffset === void 0) { indexOffset = 0; }
73
- var result = [];
74
- var index = indexOffset;
75
- var arrayIndex = array.length;
76
- var resultIndex = 0;
77
- while (--arrayIndex >= 0)
78
- result[resultIndex++] = f(array[arrayIndex], index++);
79
- return result;
80
- }
81
- exports.reverseMap = reverseMap;
82
- // Returns a copy of the given array with the given value added at the start
83
- function prepend(array, value) {
84
- var clone = array.slice();
85
- clone.unshift(value);
86
- return clone;
87
- }
88
- exports.prepend = prepend;
89
- // Returns the last element of the array
90
- function last(arr) {
91
- return arr[arr.length - 1];
92
- }
93
- exports.last = last;
94
- // Returns a copy of the array where the element at given index is replaced by the given updater.
95
- // If the new element is the same as the old element, the original array is returned
96
- function update(arr, index, updater) {
97
- if (index < 0 || index >= arr.length)
98
- return arr;
99
- var curValue = arr[index];
100
- var newValue = (0, common_1.Update)(curValue, updater);
101
- if (Object.is(newValue, curValue))
102
- return arr;
103
- var newArr = arr.slice();
104
- newArr[index] = newValue;
105
- return newArr;
106
- }
107
- exports.update = update;
108
- // Returns a copy of the array where the element at given index is replaced by applying given function.
109
- // If the new element is the same as the old element, the original array is returned
110
- function mod(arr, index, f) {
111
- if (index < 0 || index >= arr.length)
112
- return arr;
113
- var curValue = arr[index];
114
- var newValue = f(curValue);
115
- if (Object.is(newValue, curValue))
116
- return arr;
117
- var newArr = arr.slice();
118
- newArr[index] = newValue;
119
- return newArr;
120
- }
121
- exports.mod = mod;
122
- // Returns a copy of the array where at given index the given value is inserted
123
- function insert(arr, index, value) {
124
- var clone = arr.slice();
125
- clone.splice(index, 0, value);
126
- return clone;
127
- }
128
- exports.insert = insert;
129
- // Returns a copy of the array, without its first element
130
- function tail(arr) {
131
- return arr.slice(1);
132
- }
133
- exports.tail = tail;
134
- // Returns a copy of the array, without its last element
135
- function init(arr) {
136
- return arr.slice(0, arr.length - 1);
137
- }
138
- exports.init = init;
139
- // Immutable version of the array .splice command, always returns a new array
140
- function splice(arr, start, deleteCount) {
141
- var items = [];
142
- for (var _i = 3; _i < arguments.length; _i++) {
143
- items[_i - 3] = arguments[_i];
144
- }
145
- var clone = arr.slice();
146
- clone.splice.apply(clone, tslib_1.__spreadArray([start, deleteCount], tslib_1.__read(items), false));
147
- return clone;
148
- }
149
- exports.splice = splice;
150
- // Returns a copy of the array, where its 'sparse' property is kept (sparse = not all indices have a value)
151
- function copySparse(arr) {
152
- var clone = [];
153
- for (var key in arr) {
154
- clone[key] = arr[key];
155
- }
156
- return clone;
157
- }
158
- exports.copySparse = copySparse;
159
- // Returns a copy of the array with given function applied to each element, where its 'sparse' property is kept
160
- // (sparse = not all indices have a value)
161
- function mapSparse(arr, f) {
162
- var result = Array(arr.length);
163
- for (var key in arr) {
164
- result[key] = f(arr[key], key);
165
- }
166
- return result;
167
- }
168
- exports.mapSparse = mapSparse;
169
- //# sourceMappingURL=arr.js.map