@oscarpalmer/atoms 0.52.0 → 0.53.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/dist/js/array.js CHANGED
@@ -1,3 +1,8 @@
1
+ // src/js/is.ts
2
+ function isKey(value) {
3
+ return typeof value === "number" || typeof value === "string";
4
+ }
5
+
1
6
  // src/js/array.ts
2
7
  function chunk(array, size) {
3
8
  const { length } = array;
@@ -14,7 +19,12 @@ function chunk(array, size) {
14
19
  return chunks;
15
20
  }
16
21
  var comparison = function(first, second) {
17
- return [first, second].every((value) => ["bigint", "boolean", "date", "number"].includes(typeof value)) ? Number(first) - Number(second) : String(first).localeCompare(String(second));
22
+ if (typeof first === "number" && typeof second === "number") {
23
+ return first - second;
24
+ }
25
+ const firstAsNumber = Number(first);
26
+ const secondAsNumber = Number(second);
27
+ return Number.isNaN(firstAsNumber) || Number.isNaN(secondAsNumber) ? String(first).localeCompare(String(second)) : firstAsNumber - secondAsNumber;
18
28
  };
19
29
  function exists(array, value, key) {
20
30
  return findValue("index", array, value, key) > -1;
@@ -37,7 +47,7 @@ var findValue = function(type, array, value, key) {
37
47
  const { length } = array;
38
48
  for (let index = 0;index < length; index += 1) {
39
49
  const item = array[index];
40
- if (callbacks.key?.(item) === value) {
50
+ if (callbacks.key?.(item, index, array) === value) {
41
51
  return type === "index" ? index : item;
42
52
  }
43
53
  }
@@ -60,10 +70,10 @@ var findValues = function(type, array, value, key) {
60
70
  const values = hasCallback ? [] : result;
61
71
  for (let index = 0;index < length; index += 1) {
62
72
  const item = array[index];
63
- const itemValue = hasCallback ? callbacks.key?.(item) : item;
64
- if (type === "all" && itemValue === value || type === "unique" && values.indexOf(itemValue) === -1) {
73
+ const itemKey = hasCallback ? callbacks.key?.(item, index, array) : item;
74
+ if (type === "all" && itemKey === value || type === "unique" && values.indexOf(itemKey) === -1) {
65
75
  if (values !== result) {
66
- values.push(itemValue);
76
+ values.push(itemKey);
67
77
  }
68
78
  result.push(item);
69
79
  }
@@ -85,36 +95,33 @@ var getCallbacks = function(bool, key) {
85
95
  key: (value) => value?.[key]
86
96
  };
87
97
  };
88
- var getSortedValue = function(map, value, callback) {
89
- if (!map.has(value)) {
90
- map.set(value, new Map);
91
- }
92
- const stored = map.get(value);
93
- if (stored?.has(callback)) {
94
- return stored.get(callback);
95
- }
96
- const result = callback?.(value) ?? value;
97
- stored?.set(callback, result);
98
- return result;
99
- };
100
98
  function groupBy(array, key) {
99
+ return groupValues(array, key, true, false);
100
+ }
101
+ var groupValues = function(array, key, arrays, indicable) {
101
102
  const callbacks = getCallbacks(undefined, key);
102
- if (callbacks?.key == null) {
103
+ const hasCallback = typeof callbacks?.key === "function";
104
+ if (!hasCallback && !indicable) {
103
105
  return {};
104
106
  }
105
- const grouped = {};
107
+ const record = {};
106
108
  const { length } = array;
107
109
  for (let index = 0;index < length; index += 1) {
108
- const item = array[index];
109
- const value = callbacks.key(item);
110
- if (value in grouped) {
111
- grouped[value].push(item);
110
+ const value = array[index];
111
+ const key2 = hasCallback ? callbacks?.key?.(value, index, array) ?? index : index;
112
+ if (arrays) {
113
+ const existing = record[key2];
114
+ if (Array.isArray(existing)) {
115
+ existing.push(value);
116
+ } else {
117
+ record[key2] = [value];
118
+ }
112
119
  } else {
113
- grouped[value] = [item];
120
+ record[key2] = value;
114
121
  }
115
122
  }
116
- return grouped;
117
- }
123
+ return record;
124
+ };
118
125
  function indexOf(array, value, key) {
119
126
  return findValue("index", array, value, key);
120
127
  }
@@ -137,52 +144,84 @@ function push(array, values) {
137
144
  return insertValues("push", array, values, array.length, 0);
138
145
  }
139
146
  function sort(array, first, second) {
147
+ if (array.length < 2) {
148
+ return array;
149
+ }
140
150
  if (first == null || typeof first === "boolean") {
141
151
  return first === true ? array.sort((first2, second2) => second2 - first2) : array.sort();
142
152
  }
143
153
  const direction = second === true ? "desc" : "asc";
144
154
  const keys = (Array.isArray(first) ? first : [first]).map((key) => {
145
- if (typeof key === "object") {
146
- return "value" in key ? {
147
- direction: key.direction,
148
- callback: getCallbacks(null, key.value)?.key
149
- } : null;
150
- }
151
- return {
155
+ const returned = {
152
156
  direction,
153
- callback: getCallbacks(null, key)?.key
157
+ callback: undefined
154
158
  };
155
- }).filter((key) => typeof key?.callback === "function");
159
+ if (isKey(key)) {
160
+ returned.callback = (value) => value[key];
161
+ } else if (typeof key === "function") {
162
+ returned.callback = key;
163
+ } else if (typeof key?.value === "function" || isKey(key?.value)) {
164
+ returned.direction = key?.direction ?? direction;
165
+ returned.callback = typeof key.value === "function" ? key.value : (value) => value[key.value];
166
+ }
167
+ return returned;
168
+ }).filter((key) => typeof key.callback === "function");
156
169
  const { length } = keys;
157
170
  if (length === 0) {
158
- return second === true ? array.sort((first2, second2) => second2 - first2) : array.sort();
171
+ return direction === "asc" ? array.sort() : array.sort((first2, second2) => second2 - first2);
172
+ }
173
+ if (length === 1) {
174
+ return array.sort((first2, second2) => comparison(keys[0].callback(first2), keys[0].callback(second2)) * (keys[0].direction === "asc" ? 1 : -1));
159
175
  }
160
- const store = new Map;
161
176
  const sorted = array.sort((first2, second2) => {
162
177
  for (let index = 0;index < length; index += 1) {
163
178
  const { callback, direction: direction2 } = keys[index];
164
- if (callback == null) {
165
- continue;
166
- }
167
- const compared = comparison(getSortedValue(store, first2, callback), getSortedValue(store, second2, callback)) * (direction2 === "asc" ? 1 : -1);
179
+ const descending = direction2 === "desc";
180
+ const compared = comparison(callback(descending ? second2 : first2), callback(descending ? first2 : second2));
168
181
  if (compared !== 0) {
169
182
  return compared;
170
183
  }
171
184
  }
172
185
  return 0;
173
186
  });
174
- store.clear();
175
187
  return sorted;
176
188
  }
177
189
  function splice(array, start, amountOrValues, values) {
178
190
  const amoutOrValuesIsArray = Array.isArray(amountOrValues);
179
191
  return insertValues("splice", array, amoutOrValuesIsArray ? amountOrValues : values ?? [], start, amoutOrValuesIsArray ? array.length : typeof amountOrValues === "number" && amountOrValues > 0 ? amountOrValues : 0);
180
192
  }
193
+ function toMap(array, first, second) {
194
+ const asArrays = first === true || second === true;
195
+ const callbacks = getCallbacks(undefined, first);
196
+ const hasCallback = typeof callbacks?.key === "function";
197
+ const map = new Map;
198
+ const { length } = array;
199
+ for (let index = 0;index < length; index += 1) {
200
+ const value = array[index];
201
+ const key = hasCallback ? callbacks?.key?.(value, index, array) ?? index : index;
202
+ if (asArrays) {
203
+ const existing = map.get(key);
204
+ if (Array.isArray(existing)) {
205
+ existing.push(value);
206
+ } else {
207
+ map.set(key, [value]);
208
+ }
209
+ } else {
210
+ map.set(key, value);
211
+ }
212
+ }
213
+ return map;
214
+ }
215
+ function toRecord(array, first, second) {
216
+ return groupValues(array, first, first === true || second === true, true);
217
+ }
181
218
  function unique(array, key) {
182
219
  return findValues("unique", array, undefined, key);
183
220
  }
184
221
  export {
185
222
  unique,
223
+ toRecord,
224
+ toMap,
186
225
  splice,
187
226
  sort,
188
227
  push,
package/dist/js/array.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  // src/js/array.ts
2
+ import {isKey} from "./is";
2
3
  function chunk(array, size) {
3
4
  const { length } = array;
4
5
  const chunkSize = typeof size === "number" && size > 0 ? size : 32000;
@@ -14,7 +15,12 @@ function chunk(array, size) {
14
15
  return chunks;
15
16
  }
16
17
  var comparison = function(first, second) {
17
- return [first, second].every((value) => ["bigint", "boolean", "date", "number"].includes(typeof value)) ? Number(first) - Number(second) : String(first).localeCompare(String(second));
18
+ if (typeof first === "number" && typeof second === "number") {
19
+ return first - second;
20
+ }
21
+ const firstAsNumber = Number(first);
22
+ const secondAsNumber = Number(second);
23
+ return Number.isNaN(firstAsNumber) || Number.isNaN(secondAsNumber) ? String(first).localeCompare(String(second)) : firstAsNumber - secondAsNumber;
18
24
  };
19
25
  function exists(array, value, key) {
20
26
  return findValue("index", array, value, key) > -1;
@@ -37,7 +43,7 @@ var findValue = function(type, array, value, key) {
37
43
  const { length } = array;
38
44
  for (let index = 0;index < length; index += 1) {
39
45
  const item = array[index];
40
- if (callbacks.key?.(item) === value) {
46
+ if (callbacks.key?.(item, index, array) === value) {
41
47
  return type === "index" ? index : item;
42
48
  }
43
49
  }
@@ -60,10 +66,10 @@ var findValues = function(type, array, value, key) {
60
66
  const values = hasCallback ? [] : result;
61
67
  for (let index = 0;index < length; index += 1) {
62
68
  const item = array[index];
63
- const itemValue = hasCallback ? callbacks.key?.(item) : item;
64
- if (type === "all" && itemValue === value || type === "unique" && values.indexOf(itemValue) === -1) {
69
+ const itemKey = hasCallback ? callbacks.key?.(item, index, array) : item;
70
+ if (type === "all" && itemKey === value || type === "unique" && values.indexOf(itemKey) === -1) {
65
71
  if (values !== result) {
66
- values.push(itemValue);
72
+ values.push(itemKey);
67
73
  }
68
74
  result.push(item);
69
75
  }
@@ -85,36 +91,33 @@ var getCallbacks = function(bool, key) {
85
91
  key: (value) => value?.[key]
86
92
  };
87
93
  };
88
- var getSortedValue = function(map, value, callback) {
89
- if (!map.has(value)) {
90
- map.set(value, new Map);
91
- }
92
- const stored = map.get(value);
93
- if (stored?.has(callback)) {
94
- return stored.get(callback);
95
- }
96
- const result = callback?.(value) ?? value;
97
- stored?.set(callback, result);
98
- return result;
99
- };
100
94
  function groupBy(array, key) {
95
+ return groupValues(array, key, true, false);
96
+ }
97
+ var groupValues = function(array, key, arrays, indicable) {
101
98
  const callbacks = getCallbacks(undefined, key);
102
- if (callbacks?.key == null) {
99
+ const hasCallback = typeof callbacks?.key === "function";
100
+ if (!hasCallback && !indicable) {
103
101
  return {};
104
102
  }
105
- const grouped = {};
103
+ const record = {};
106
104
  const { length } = array;
107
105
  for (let index = 0;index < length; index += 1) {
108
- const item = array[index];
109
- const value = callbacks.key(item);
110
- if (value in grouped) {
111
- grouped[value].push(item);
106
+ const value = array[index];
107
+ const key2 = hasCallback ? callbacks?.key?.(value, index, array) ?? index : index;
108
+ if (arrays) {
109
+ const existing = record[key2];
110
+ if (Array.isArray(existing)) {
111
+ existing.push(value);
112
+ } else {
113
+ record[key2] = [value];
114
+ }
112
115
  } else {
113
- grouped[value] = [item];
116
+ record[key2] = value;
114
117
  }
115
118
  }
116
- return grouped;
117
- }
119
+ return record;
120
+ };
118
121
  function indexOf(array, value, key) {
119
122
  return findValue("index", array, value, key);
120
123
  }
@@ -137,52 +140,84 @@ function push(array, values) {
137
140
  return insertValues("push", array, values, array.length, 0);
138
141
  }
139
142
  function sort(array, first, second) {
143
+ if (array.length < 2) {
144
+ return array;
145
+ }
140
146
  if (first == null || typeof first === "boolean") {
141
147
  return first === true ? array.sort((first2, second2) => second2 - first2) : array.sort();
142
148
  }
143
149
  const direction = second === true ? "desc" : "asc";
144
150
  const keys = (Array.isArray(first) ? first : [first]).map((key) => {
145
- if (typeof key === "object") {
146
- return "value" in key ? {
147
- direction: key.direction,
148
- callback: getCallbacks(null, key.value)?.key
149
- } : null;
150
- }
151
- return {
151
+ const returned = {
152
152
  direction,
153
- callback: getCallbacks(null, key)?.key
153
+ callback: undefined
154
154
  };
155
- }).filter((key) => typeof key?.callback === "function");
155
+ if (isKey(key)) {
156
+ returned.callback = (value) => value[key];
157
+ } else if (typeof key === "function") {
158
+ returned.callback = key;
159
+ } else if (typeof key?.value === "function" || isKey(key?.value)) {
160
+ returned.direction = key?.direction ?? direction;
161
+ returned.callback = typeof key.value === "function" ? key.value : (value) => value[key.value];
162
+ }
163
+ return returned;
164
+ }).filter((key) => typeof key.callback === "function");
156
165
  const { length } = keys;
157
166
  if (length === 0) {
158
- return second === true ? array.sort((first2, second2) => second2 - first2) : array.sort();
167
+ return direction === "asc" ? array.sort() : array.sort((first2, second2) => second2 - first2);
168
+ }
169
+ if (length === 1) {
170
+ return array.sort((first2, second2) => comparison(keys[0].callback(first2), keys[0].callback(second2)) * (keys[0].direction === "asc" ? 1 : -1));
159
171
  }
160
- const store = new Map;
161
172
  const sorted = array.sort((first2, second2) => {
162
173
  for (let index = 0;index < length; index += 1) {
163
174
  const { callback, direction: direction2 } = keys[index];
164
- if (callback == null) {
165
- continue;
166
- }
167
- const compared = comparison(getSortedValue(store, first2, callback), getSortedValue(store, second2, callback)) * (direction2 === "asc" ? 1 : -1);
175
+ const descending = direction2 === "desc";
176
+ const compared = comparison(callback(descending ? second2 : first2), callback(descending ? first2 : second2));
168
177
  if (compared !== 0) {
169
178
  return compared;
170
179
  }
171
180
  }
172
181
  return 0;
173
182
  });
174
- store.clear();
175
183
  return sorted;
176
184
  }
177
185
  function splice(array, start, amountOrValues, values) {
178
186
  const amoutOrValuesIsArray = Array.isArray(amountOrValues);
179
187
  return insertValues("splice", array, amoutOrValuesIsArray ? amountOrValues : values ?? [], start, amoutOrValuesIsArray ? array.length : typeof amountOrValues === "number" && amountOrValues > 0 ? amountOrValues : 0);
180
188
  }
189
+ function toMap(array, first, second) {
190
+ const asArrays = first === true || second === true;
191
+ const callbacks = getCallbacks(undefined, first);
192
+ const hasCallback = typeof callbacks?.key === "function";
193
+ const map = new Map;
194
+ const { length } = array;
195
+ for (let index = 0;index < length; index += 1) {
196
+ const value = array[index];
197
+ const key = hasCallback ? callbacks?.key?.(value, index, array) ?? index : index;
198
+ if (asArrays) {
199
+ const existing = map.get(key);
200
+ if (Array.isArray(existing)) {
201
+ existing.push(value);
202
+ } else {
203
+ map.set(key, [value]);
204
+ }
205
+ } else {
206
+ map.set(key, value);
207
+ }
208
+ }
209
+ return map;
210
+ }
211
+ function toRecord(array, first, second) {
212
+ return groupValues(array, first, first === true || second === true, true);
213
+ }
181
214
  function unique(array, key) {
182
215
  return findValues("unique", array, undefined, key);
183
216
  }
184
217
  export {
185
218
  unique,
219
+ toRecord,
220
+ toMap,
186
221
  splice,
187
222
  sort,
188
223
  push,
@@ -1,6 +1,37 @@
1
1
  // src/js/function.ts
2
+ function memoise(callback) {
3
+ function get(...parameters) {
4
+ const key = parameters[0];
5
+ if (cache.has(key)) {
6
+ return cache.get(key);
7
+ }
8
+ const value = callback(...parameters);
9
+ cache.set(key, value);
10
+ return value;
11
+ }
12
+ const cache = new Map;
13
+ return Object.create({
14
+ cache,
15
+ clear() {
16
+ cache.clear();
17
+ },
18
+ delete(key) {
19
+ return cache.delete(key);
20
+ },
21
+ get(key) {
22
+ return cache.get(key);
23
+ },
24
+ has(key) {
25
+ return cache.has(key);
26
+ },
27
+ run(...parameters) {
28
+ return get(...parameters);
29
+ }
30
+ });
31
+ }
2
32
  function noop() {
3
33
  }
4
34
  export {
5
- noop
35
+ noop,
36
+ memoise
6
37
  };
@@ -1,6 +1,37 @@
1
1
  // src/js/function.ts
2
+ function memoise(callback) {
3
+ function get(...parameters) {
4
+ const key = parameters[0];
5
+ if (cache.has(key)) {
6
+ return cache.get(key);
7
+ }
8
+ const value = callback(...parameters);
9
+ cache.set(key, value);
10
+ return value;
11
+ }
12
+ const cache = new Map;
13
+ return Object.create({
14
+ cache,
15
+ clear() {
16
+ cache.clear();
17
+ },
18
+ delete(key) {
19
+ return cache.delete(key);
20
+ },
21
+ get(key) {
22
+ return cache.get(key);
23
+ },
24
+ has(key) {
25
+ return cache.has(key);
26
+ },
27
+ run(...parameters) {
28
+ return get(...parameters);
29
+ }
30
+ });
31
+ }
2
32
  function noop() {
3
33
  }
4
34
  export {
5
- noop
35
+ noop,
36
+ memoise
6
37
  };