@zag-js/utils 0.1.5 → 0.2.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/index.js CHANGED
@@ -1,3 +1,58 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ add: () => add,
24
+ callAll: () => callAll,
25
+ cast: () => cast,
26
+ chunk: () => chunk,
27
+ clear: () => clear,
28
+ first: () => first,
29
+ fromLength: () => fromLength,
30
+ has: () => has,
31
+ hasProp: () => hasProp,
32
+ invariant: () => invariant,
33
+ isArray: () => isArray,
34
+ isBoolean: () => isBoolean,
35
+ isDev: () => isDev,
36
+ isEmpty: () => isEmpty,
37
+ isFunction: () => isFunction,
38
+ isNumber: () => isNumber,
39
+ isObject: () => isObject,
40
+ isString: () => isString,
41
+ last: () => last,
42
+ next: () => next,
43
+ nextIndex: () => nextIndex,
44
+ noop: () => noop,
45
+ prev: () => prev,
46
+ prevIndex: () => prevIndex,
47
+ remove: () => remove,
48
+ removeAt: () => removeAt,
49
+ runIfFn: () => runIfFn,
50
+ toArray: () => toArray,
51
+ uuid: () => uuid,
52
+ warn: () => warn
53
+ });
54
+ module.exports = __toCommonJS(src_exports);
55
+
1
56
  // src/array.ts
2
57
  function toArray(v) {
3
58
  if (!v)
@@ -59,7 +114,7 @@ var chunk = (v, size) => {
59
114
  // src/functions.ts
60
115
  var runIfFn = (v, ...a) => {
61
116
  const res = typeof v === "function" ? v(...a) : v;
62
- return res ?? void 0;
117
+ return res != null ? res : void 0;
63
118
  };
64
119
  var cast = (v) => v;
65
120
  var noop = () => {
@@ -102,7 +157,8 @@ function invariant(...a) {
102
157
  throw new Error(m);
103
158
  }
104
159
  }
105
- export {
160
+ // Annotate the CommonJS export names for ESM import in node:
161
+ 0 && (module.exports = {
106
162
  add,
107
163
  callAll,
108
164
  cast,
@@ -133,4 +189,4 @@ export {
133
189
  toArray,
134
190
  uuid,
135
191
  warn
136
- };
192
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,136 @@
1
+ // src/array.ts
2
+ function toArray(v) {
3
+ if (!v)
4
+ return [];
5
+ return Array.isArray(v) ? v : [v];
6
+ }
7
+ var fromLength = (length) => Array.from(Array(length).keys());
8
+ var first = (v) => v[0];
9
+ var last = (v) => v[v.length - 1];
10
+ var isEmpty = (v) => v.length === 0;
11
+ var has = (v, t) => v.indexOf(t) !== -1;
12
+ var add = (v, ...items) => v.concat(items);
13
+ var remove = (v, item) => removeAt(v, v.indexOf(item));
14
+ var removeAt = (v, i) => {
15
+ if (i > -1)
16
+ v.splice(i, 1);
17
+ return v;
18
+ };
19
+ function clear(v) {
20
+ while (v.length > 0)
21
+ v.pop();
22
+ return v;
23
+ }
24
+ function nextIndex(v, idx, opts = {}) {
25
+ const { step = 1, loop = true } = opts;
26
+ const next2 = idx + step;
27
+ const len = v.length;
28
+ const last2 = len - 1;
29
+ if (idx === -1)
30
+ return step > 0 ? 0 : last2;
31
+ if (next2 < 0)
32
+ return loop ? last2 : 0;
33
+ if (next2 >= len)
34
+ return loop ? 0 : idx > len ? len : idx;
35
+ return next2;
36
+ }
37
+ function next(v, idx, opts = {}) {
38
+ return v[nextIndex(v, idx, opts)];
39
+ }
40
+ function prevIndex(v, idx, opts = {}) {
41
+ const { step = 1, loop = true } = opts;
42
+ return nextIndex(v, idx, { step: -step, loop });
43
+ }
44
+ function prev(v, index, opts = {}) {
45
+ return v[prevIndex(v, index, opts)];
46
+ }
47
+ var chunk = (v, size) => {
48
+ const res = [];
49
+ return v.reduce((rows, value, index) => {
50
+ var _a;
51
+ if (index % size === 0)
52
+ rows.push([value]);
53
+ else
54
+ (_a = last(rows)) == null ? void 0 : _a.push(value);
55
+ return rows;
56
+ }, res);
57
+ };
58
+
59
+ // src/functions.ts
60
+ var runIfFn = (v, ...a) => {
61
+ const res = typeof v === "function" ? v(...a) : v;
62
+ return res != null ? res : void 0;
63
+ };
64
+ var cast = (v) => v;
65
+ var noop = () => {
66
+ };
67
+ var callAll = (...fns) => (...a) => {
68
+ fns.forEach(function(fn) {
69
+ fn == null ? void 0 : fn(...a);
70
+ });
71
+ };
72
+ var uuid = /* @__PURE__ */ (() => {
73
+ let id = 0;
74
+ return () => {
75
+ id++;
76
+ return id.toString(36);
77
+ };
78
+ })();
79
+
80
+ // src/guard.ts
81
+ var isDev = () => process.env.NODE_ENV !== "production";
82
+ var isArray = (v) => Array.isArray(v);
83
+ var isBoolean = (v) => v === true || v === false;
84
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
85
+ var isNumber = (v) => typeof v === "number" && !Number.isNaN(v);
86
+ var isString = (v) => typeof v === "string";
87
+ var isFunction = (v) => typeof v === "function";
88
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
89
+
90
+ // src/warning.ts
91
+ function warn(...a) {
92
+ const m = a.length === 1 ? a[0] : a[1];
93
+ const c = a.length === 2 ? a[0] : true;
94
+ if (c && process.env.NODE_ENV !== "production") {
95
+ console.warn(m);
96
+ }
97
+ }
98
+ function invariant(...a) {
99
+ const m = a.length === 1 ? a[0] : a[1];
100
+ const c = a.length === 2 ? a[0] : true;
101
+ if (c && process.env.NODE_ENV !== "production") {
102
+ throw new Error(m);
103
+ }
104
+ }
105
+ export {
106
+ add,
107
+ callAll,
108
+ cast,
109
+ chunk,
110
+ clear,
111
+ first,
112
+ fromLength,
113
+ has,
114
+ hasProp,
115
+ invariant,
116
+ isArray,
117
+ isBoolean,
118
+ isDev,
119
+ isEmpty,
120
+ isFunction,
121
+ isNumber,
122
+ isObject,
123
+ isString,
124
+ last,
125
+ next,
126
+ nextIndex,
127
+ noop,
128
+ prev,
129
+ prevIndex,
130
+ remove,
131
+ removeAt,
132
+ runIfFn,
133
+ toArray,
134
+ uuid,
135
+ warn
136
+ };
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
- "type": "module",
3
2
  "name": "@zag-js/utils",
4
- "version": "0.1.5",
3
+ "version": "0.2.0",
5
4
  "description": "",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
6
8
  "keywords": [
7
9
  "js",
8
10
  "utils",
@@ -11,8 +13,6 @@
11
13
  "author": "Segun Adebayo <sage@adebayosegun.com>",
12
14
  "homepage": "https://github.com/chakra-ui/zag#readme",
13
15
  "license": "MIT",
14
- "main": "dist/index.js",
15
- "types": "dist/index.d.ts",
16
16
  "repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/core",
17
17
  "sideEffects": false,
18
18
  "files": [
@@ -25,9 +25,9 @@
25
25
  "url": "https://github.com/chakra-ui/zag/issues"
26
26
  },
27
27
  "scripts": {
28
- "build-fast": "tsup src/index.ts --format=esm",
28
+ "build-fast": "tsup src/index.ts --format=esm,cjs",
29
29
  "start": "pnpm build --watch",
30
- "build": "tsup src/index.ts --format=esm --dts",
30
+ "build": "tsup src/index.ts --format=esm,cjs --dts",
31
31
  "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
32
32
  "lint": "eslint src --ext .ts,.tsx",
33
33
  "test-ci": "pnpm test --ci --runInBand",