@zag-js/utils 0.9.1 → 0.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/utils",
3
- "version": "0.9.1",
3
+ "version": "0.10.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "js",
@@ -13,7 +13,8 @@
13
13
  "repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/core",
14
14
  "sideEffects": false,
15
15
  "files": [
16
- "dist/**/*"
16
+ "dist",
17
+ "src"
17
18
  ],
18
19
  "publishConfig": {
19
20
  "access": "public"
package/src/array.ts ADDED
@@ -0,0 +1,66 @@
1
+ export function toArray<T>(v: T | T[] | undefined | null): T[] {
2
+ if (!v) return []
3
+ return Array.isArray(v) ? v : [v]
4
+ }
5
+
6
+ export const fromLength = (length: number) => Array.from(Array(length).keys())
7
+
8
+ export const first = <T>(v: T[]): T | undefined => v[0]
9
+
10
+ export const last = <T>(v: T[]): T | undefined => v[v.length - 1]
11
+
12
+ export const isEmpty = <T>(v: T[]): boolean => v.length === 0
13
+
14
+ export const has = <T>(v: T[], t: any): boolean => v.indexOf(t) !== -1
15
+
16
+ export const add = <T>(v: T[], ...items: T[]): T[] => v.concat(items)
17
+
18
+ export const remove = <T>(v: T[], item: T): T[] => removeAt(v, v.indexOf(item))
19
+
20
+ export const removeAt = <T>(v: T[], i: number): T[] => {
21
+ if (i > -1) v.splice(i, 1)
22
+ return v
23
+ }
24
+
25
+ export function clear<T>(v: T[]): T[] {
26
+ while (v.length > 0) v.pop()
27
+ return v
28
+ }
29
+
30
+ export type IndexOptions = {
31
+ step?: number
32
+ loop?: boolean
33
+ }
34
+
35
+ export function nextIndex<T>(v: T[], idx: number, opts: IndexOptions = {}): number {
36
+ const { step = 1, loop = true } = opts
37
+ const next = idx + step
38
+ const len = v.length
39
+ const last = len - 1
40
+ if (idx === -1) return step > 0 ? 0 : last
41
+ if (next < 0) return loop ? last : 0
42
+ if (next >= len) return loop ? 0 : idx > len ? len : idx
43
+ return next
44
+ }
45
+
46
+ export function next<T>(v: T[], idx: number, opts: IndexOptions = {}): T | undefined {
47
+ return v[nextIndex(v, idx, opts)]
48
+ }
49
+
50
+ export function prevIndex<T>(v: T[], idx: number, opts: IndexOptions = {}): number {
51
+ const { step = 1, loop = true } = opts
52
+ return nextIndex(v, idx, { step: -step, loop })
53
+ }
54
+
55
+ export function prev<T>(v: T[], index: number, opts: IndexOptions = {}): T | undefined {
56
+ return v[prevIndex(v, index, opts)]
57
+ }
58
+
59
+ export const chunk = <T>(v: T[], size: number): T[][] => {
60
+ const res: T[][] = []
61
+ return v.reduce((rows, value, index) => {
62
+ if (index % size === 0) rows.push([value])
63
+ else last(rows)?.push(value)
64
+ return rows
65
+ }, res)
66
+ }
@@ -0,0 +1,27 @@
1
+ export const runIfFn = <T>(
2
+ v: T | undefined,
3
+ ...a: T extends (...a: any[]) => void ? Parameters<T> : never
4
+ ): T extends (...a: any[]) => void ? NonNullable<ReturnType<T>> : NonNullable<T> => {
5
+ const res = typeof v === "function" ? v(...a) : v
6
+ return res ?? undefined
7
+ }
8
+
9
+ export const cast = <T>(v: unknown): T => v as T
10
+
11
+ export const noop = () => {}
12
+
13
+ export const callAll =
14
+ <T extends (...a: any[]) => void>(...fns: (T | undefined)[]) =>
15
+ (...a: Parameters<T>) => {
16
+ fns.forEach(function (fn) {
17
+ fn?.(...a)
18
+ })
19
+ }
20
+
21
+ export const uuid = /*#__PURE__*/ (() => {
22
+ let id = 0
23
+ return () => {
24
+ id++
25
+ return id.toString(36)
26
+ }
27
+ })()
package/src/guard.ts ADDED
@@ -0,0 +1,10 @@
1
+ export const isDev = () => process.env.NODE_ENV !== "production"
2
+ export const isArray = (v: any): v is any[] => Array.isArray(v)
3
+ export const isBoolean = (v: any): v is boolean => v === true || v === false
4
+ export const isObject = (v: any): v is Record<string, any> => !(v == null || typeof v !== "object" || isArray(v))
5
+ export const isNumber = (v: any): v is number => typeof v === "number" && !Number.isNaN(v)
6
+ export const isString = (v: any): v is string => typeof v === "string"
7
+ export const isFunction = (v: any): v is Function => typeof v === "function"
8
+
9
+ export const hasProp = <T extends string>(obj: any, prop: T): obj is Record<T, any> =>
10
+ Object.prototype.hasOwnProperty.call(obj, prop)
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./array"
2
+ export * from "./functions"
3
+ export * from "./guard"
4
+ export * from "./object"
5
+ export * from "./warning"
package/src/object.ts ADDED
@@ -0,0 +1,23 @@
1
+ export function compact<T extends Record<string, unknown> | undefined>(obj: T): T {
2
+ if (!isPlainObject(obj) || obj === undefined) {
3
+ return obj
4
+ }
5
+
6
+ const keys = Reflect.ownKeys(obj).filter((key) => typeof key === "string")
7
+ const filtered: Partial<T> = {}
8
+ for (const key of keys) {
9
+ const value = (obj as any)[key]
10
+ if (value !== undefined) {
11
+ filtered[key as keyof T] = compact(value)
12
+ }
13
+ }
14
+ return filtered as T
15
+ }
16
+
17
+ export function json(value: any) {
18
+ return JSON.parse(JSON.stringify(value))
19
+ }
20
+
21
+ const isPlainObject = (value: any) => {
22
+ return value && typeof value === "object" && value.constructor === Object
23
+ }
package/src/warning.ts ADDED
@@ -0,0 +1,19 @@
1
+ export function warn(m: string): void
2
+ export function warn(c: boolean, m: string): void
3
+ export function warn(...a: any[]): void {
4
+ const m = a.length === 1 ? a[0] : a[1]
5
+ const c = a.length === 2 ? a[0] : true
6
+ if (c && process.env.NODE_ENV !== "production") {
7
+ console.warn(m)
8
+ }
9
+ }
10
+
11
+ export function invariant(m: string): void
12
+ export function invariant(c: boolean, m: string): void
13
+ export function invariant(...a: any[]): void {
14
+ const m = a.length === 1 ? a[0] : a[1]
15
+ const c = a.length === 2 ? a[0] : true
16
+ if (c && process.env.NODE_ENV !== "production") {
17
+ throw new Error(m)
18
+ }
19
+ }