@solid-primitives/utils 0.2.1 → 0.2.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.
package/dist/index.cjs CHANGED
@@ -31,17 +31,21 @@ __export(src_exports, {
31
31
  asArray: () => asArray,
32
32
  compare: () => compare,
33
33
  createCallbackStack: () => createCallbackStack,
34
+ createProxy: () => createProxy,
34
35
  destore: () => destore,
35
36
  entries: () => entries,
36
37
  forEach: () => forEach,
38
+ forEachEntry: () => forEachEntry,
37
39
  includes: () => includes,
38
40
  isArray: () => isArray,
39
41
  isBoolean: () => isBoolean,
40
42
  isClient: () => isClient,
41
43
  isDefined: () => isDefined,
44
+ isDev: () => isDev,
42
45
  isFunction: () => isFunction,
43
46
  isNumber: () => isNumber,
44
47
  isObject: () => isObject,
48
+ isProd: () => isProd,
45
49
  isServer: () => import_web.isServer,
46
50
  isString: () => isString,
47
51
  keys: () => keys,
@@ -52,19 +56,23 @@ __export(src_exports, {
52
56
  promiseTimeout: () => promiseTimeout,
53
57
  raceTimeout: () => raceTimeout,
54
58
  tuple: () => tuple,
59
+ warn: () => warn,
55
60
  withAccess: () => withAccess
56
61
  });
57
62
  var import_solid_js = require("solid-js");
58
63
  var import_web = require("solid-js/web");
59
64
  var noop = () => void 0;
60
65
  var isClient = !import_web.isServer;
66
+ var isDev = import_solid_js.DEV && import_solid_js.DEV.hasOwnProperty("writeSignal");
67
+ var isProd = !isDev;
68
+ var warn = (...a) => isDev && console.warn(...a);
61
69
  var isDefined = (value) => typeof value !== "undefined" && value !== null;
62
70
  var isFunction = (value) => typeof value === "function";
63
71
  var isBoolean = (val) => typeof val === "boolean";
64
72
  var isNumber = (val) => typeof val === "number";
65
73
  var isString = (val) => typeof val === "string";
66
74
  var isObject = (val) => toString.call(val) === "[object Object]";
67
- var isArray = (val) => Array.isArray(val);
75
+ var isArray = Array.isArray;
68
76
  var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
69
77
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
70
78
  var tuple = (input) => input;
@@ -89,6 +97,10 @@ function onAccess(deps, fn, options) {
89
97
  return (0, import_solid_js.on)(source, fn, options);
90
98
  }
91
99
  var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
100
+ var forEachEntry = (object, iterator) => {
101
+ const obj = access(object);
102
+ Object.entries(obj).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, obj));
103
+ };
92
104
  var entries = (object) => Object.entries(access(object));
93
105
  var keys = (object) => Object.keys(access(object));
94
106
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
@@ -114,13 +126,23 @@ var createCallbackStack = () => {
114
126
  const clear = () => stack = [];
115
127
  return {
116
128
  push: (...callbacks) => stack.push(...callbacks),
117
- execute: (arg0, arg1, arg2, arg3) => {
129
+ execute(arg0, arg1, arg2, arg3) {
118
130
  stack.forEach((cb) => cb(arg0, arg1, arg2, arg3));
119
131
  clear();
120
132
  },
121
133
  clear
122
134
  };
123
135
  };
136
+ function createProxy(traps) {
137
+ return new Proxy({}, {
138
+ get: (_, k) => traps.get(k),
139
+ set: (_, k, v) => {
140
+ var _a;
141
+ (_a = traps.set) == null ? void 0 : _a.call(traps, k, v);
142
+ return false;
143
+ }
144
+ });
145
+ }
124
146
  module.exports = __toCommonJS(src_exports);
125
147
  // Annotate the CommonJS export names for ESM import in node:
126
148
  0 && (module.exports = {
@@ -131,17 +153,21 @@ module.exports = __toCommonJS(src_exports);
131
153
  asArray,
132
154
  compare,
133
155
  createCallbackStack,
156
+ createProxy,
134
157
  destore,
135
158
  entries,
136
159
  forEach,
160
+ forEachEntry,
137
161
  includes,
138
162
  isArray,
139
163
  isBoolean,
140
164
  isClient,
141
165
  isDefined,
166
+ isDev,
142
167
  isFunction,
143
168
  isNumber,
144
169
  isObject,
170
+ isProd,
145
171
  isServer,
146
172
  isString,
147
173
  keys,
@@ -152,5 +178,6 @@ module.exports = __toCommonJS(src_exports);
152
178
  promiseTimeout,
153
179
  raceTimeout,
154
180
  tuple,
181
+ warn,
155
182
  withAccess
156
183
  });
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export { isServer } from 'solid-js/web';
8
8
  */
9
9
  declare type Fn<R = void> = () => R;
10
10
  declare type Get<T> = (v: T) => void;
11
+ declare type Clear = () => void;
11
12
  /**
12
13
  * Can be single or in an array
13
14
  */
@@ -69,11 +70,21 @@ declare type Falsy<T> = Extract<T, FalsyValue>;
69
70
  declare type Destore<T extends Object> = {
70
71
  [K in keyof T]: T[K] extends Function ? T[K] : Accessor<T[K]>;
71
72
  };
73
+ declare type Position = {
74
+ x: number;
75
+ y: number;
76
+ };
72
77
 
73
78
  /** no operation */
74
79
  declare const noop: Noop;
75
80
  declare const isClient = true;
76
81
 
82
+ /** development environment */
83
+ declare const isDev: boolean;
84
+ /** production environment */
85
+ declare const isProd: boolean;
86
+ /** `console.warn` only during development */
87
+ declare const warn: typeof console.warn;
77
88
  /**
78
89
  * `if (typeof value !== "undefined" && value !== null)`
79
90
  */
@@ -143,9 +154,13 @@ declare function onAccess<S extends MaybeAccessor<unknown>[] | [], Next, Init =
143
154
  */
144
155
  declare const forEach: <A extends unknown, V = MaybeAccessorValue<A>>(array: A, iterator: (item: V extends any[] ? ItemsOf<V> : V, index: number, array: V extends any[] ? V : V[]) => void) => void;
145
156
  /**
146
- * Get `Object.entries()` of an MaybeAccessor<Object>
157
+ * Iterate through object entries.
158
+ */
159
+ declare const forEachEntry: <A extends MaybeAccessor<object>, O = MaybeAccessorValue<A>>(object: A, iterator: (key: keyof O, item: Values<O>, index: number, pairs: [keyof O, Values<O>][], object: O) => void) => void;
160
+ /**
161
+ * Get `Object.entries()` of an MaybeAccessor<object>
147
162
  */
148
- declare const entries: <A extends MaybeAccessor<object>, V = MaybeAccessorValue<A>>(object: A) => [string, Values<V>][];
163
+ declare const entries: <A extends MaybeAccessor<object>, O = MaybeAccessorValue<A>>(object: A) => [keyof O, Values<O>][];
149
164
  /**
150
165
  * Get `Object.keys()` of an MaybeAccessor<Object>
151
166
  */
@@ -209,9 +224,18 @@ declare const onRootCleanup: typeof onCleanup;
209
224
  */
210
225
  declare function destore<T extends Object>(store: Store<T>): Destore<T>;
211
226
  declare const createCallbackStack: <A0 = void, A1 = void, A2 = void, A3 = void>() => {
212
- push: (...callbacks: Fn[]) => void;
227
+ push: (...callbacks: ((arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void)[]) => void;
213
228
  execute: (arg0: A0, arg1: A1, arg2: A2, arg3: A3) => void;
214
229
  clear: Fn;
215
230
  };
231
+ /** WIP: an easier to setup and type Proxy */
232
+ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
233
+ get: <K extends keyof T>(key: K) => T[K];
234
+ set: <K extends keyof T>(key: K, value: T[K]) => void;
235
+ }): T;
236
+ declare function createProxy<T extends Record<string | symbol, any>>(traps: {
237
+ get: <K extends keyof T>(key: K) => T[K];
238
+ set?: undefined;
239
+ }): Readonly<T>;
216
240
 
217
- export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, DeepPartialAny, Destore, Directive, ExtractIfPossible, Falsy, FalsyValue, Fn, Get, ItemsOf, ItemsOfMany, Keys, LiteralKey, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Noop, OnAccessEffectFunction, PrimitiveValue, Truthy, UnionToIntersection, Values, access, accessArray, accessAsArray, asAccessor, asArray, compare, createCallbackStack, destore, entries, forEach, includes, isArray, isBoolean, isClient, isDefined, isFunction, isNumber, isObject, isString, keys, noop, ofClass, onAccess, onRootCleanup, promiseTimeout, raceTimeout, tuple, withAccess };
241
+ export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, Clear, DeepPartialAny, Destore, Directive, ExtractIfPossible, Falsy, FalsyValue, Fn, Get, ItemsOf, ItemsOfMany, Keys, LiteralKey, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Noop, OnAccessEffectFunction, Position, PrimitiveValue, Truthy, UnionToIntersection, Values, access, accessArray, accessAsArray, asAccessor, asArray, compare, createCallbackStack, createProxy, destore, entries, forEach, forEachEntry, includes, isArray, isBoolean, isClient, isDefined, isDev, isFunction, isNumber, isObject, isProd, isString, keys, noop, ofClass, onAccess, onRootCleanup, promiseTimeout, raceTimeout, tuple, warn, withAccess };
package/dist/index.js CHANGED
@@ -1,15 +1,18 @@
1
1
  // src/index.ts
2
- import { getOwner, onCleanup, on } from "solid-js";
2
+ import { getOwner, onCleanup, on, DEV } from "solid-js";
3
3
  import { isServer } from "solid-js/web";
4
4
  var noop = () => void 0;
5
5
  var isClient = !isServer;
6
+ var isDev = DEV && DEV.hasOwnProperty("writeSignal");
7
+ var isProd = !isDev;
8
+ var warn = (...a) => isDev && console.warn(...a);
6
9
  var isDefined = (value) => typeof value !== "undefined" && value !== null;
7
10
  var isFunction = (value) => typeof value === "function";
8
11
  var isBoolean = (val) => typeof val === "boolean";
9
12
  var isNumber = (val) => typeof val === "number";
10
13
  var isString = (val) => typeof val === "string";
11
14
  var isObject = (val) => toString.call(val) === "[object Object]";
12
- var isArray = (val) => Array.isArray(val);
15
+ var isArray = Array.isArray;
13
16
  var ofClass = (v, c) => v instanceof c || v && v.constructor === c;
14
17
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
15
18
  var tuple = (input) => input;
@@ -34,6 +37,10 @@ function onAccess(deps, fn, options) {
34
37
  return on(source, fn, options);
35
38
  }
36
39
  var forEach = (array, iterator) => accessAsArray(array).forEach(iterator);
40
+ var forEachEntry = (object, iterator) => {
41
+ const obj = access(object);
42
+ Object.entries(obj).forEach(([key, item], index, pairs) => iterator(key, item, index, pairs, obj));
43
+ };
37
44
  var entries = (object) => Object.entries(access(object));
38
45
  var keys = (object) => Object.keys(access(object));
39
46
  var promiseTimeout = (ms, throwOnTimeout = false, reason = "Timeout") => new Promise((resolve, reject) => throwOnTimeout ? setTimeout(() => reject(reason), ms) : setTimeout(resolve, ms));
@@ -59,13 +66,23 @@ var createCallbackStack = () => {
59
66
  const clear = () => stack = [];
60
67
  return {
61
68
  push: (...callbacks) => stack.push(...callbacks),
62
- execute: (arg0, arg1, arg2, arg3) => {
69
+ execute(arg0, arg1, arg2, arg3) {
63
70
  stack.forEach((cb) => cb(arg0, arg1, arg2, arg3));
64
71
  clear();
65
72
  },
66
73
  clear
67
74
  };
68
75
  };
76
+ function createProxy(traps) {
77
+ return new Proxy({}, {
78
+ get: (_, k) => traps.get(k),
79
+ set: (_, k, v) => {
80
+ var _a;
81
+ (_a = traps.set) == null ? void 0 : _a.call(traps, k, v);
82
+ return false;
83
+ }
84
+ });
85
+ }
69
86
  export {
70
87
  access,
71
88
  accessArray,
@@ -74,17 +91,21 @@ export {
74
91
  asArray,
75
92
  compare,
76
93
  createCallbackStack,
94
+ createProxy,
77
95
  destore,
78
96
  entries,
79
97
  forEach,
98
+ forEachEntry,
80
99
  includes,
81
100
  isArray,
82
101
  isBoolean,
83
102
  isClient,
84
103
  isDefined,
104
+ isDev,
85
105
  isFunction,
86
106
  isNumber,
87
107
  isObject,
108
+ isProd,
88
109
  isServer,
89
110
  isString,
90
111
  keys,
@@ -95,5 +116,6 @@ export {
95
116
  promiseTimeout,
96
117
  raceTimeout,
97
118
  tuple,
119
+ warn,
98
120
  withAccess
99
121
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "A bunch of reactive utility types and functions, for building primitives with Solid.js",
5
5
  "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>",
6
6
  "license": "MIT",
@@ -9,6 +9,9 @@
9
9
  "type": "git",
10
10
  "url": "git+https://github.com/davedbase/solid-primitives.git"
11
11
  },
12
+ "bugs": {
13
+ "url": "https://github.com/davedbase/solid-primitives/issues"
14
+ },
12
15
  "private": false,
13
16
  "sideEffects": false,
14
17
  "type": "module",
@@ -44,4 +47,4 @@
44
47
  "peerDependencies": {
45
48
  "solid-js": "^1.3.1"
46
49
  }
47
- }
50
+ }