@tolki/path 0.0.0-stage → 1.0.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.
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Get the values a pluck wildcard segment iterates over, mirroring
3
+ * `data_get()`'s `is_iterable()` check (`helpers.php:90-94`): a PHP
4
+ * `foreach` walks both arrays and associative arrays, so both a JS array
5
+ * and a plain object count here — deliberately not `getAccessibleValues`,
6
+ * which only expands arrays.
7
+ *
8
+ * @param target - The value a `*` segment is expanding; callers bail to
9
+ * `null` before this runs for anything that isn't an array or an object.
10
+ * @returns The values to recurse into.
11
+ */
12
+ export declare function getPluckWildcardValues(target: unknown[] | Record<PropertyKey, unknown> | ReadonlyMap<unknown, unknown>): unknown[];
13
+ /**
14
+ * Resolve a pluck path against a single item the way `data_get` does.
15
+ *
16
+ * @param item - The item to resolve the path against.
17
+ * @param segments - The already-split path segments, each read as one literal key.
18
+ * @returns The resolved value, an array of values for a `*` segment, or null.
19
+ */
20
+ export declare function resolvePluckPath(item: unknown, segments: readonly string[]): unknown;
21
+ /**
22
+ * Determine whether a pluck path names a key at every segment, the way `data_has` does.
23
+ *
24
+ * @param item - The item to check the path against.
25
+ * @param segments - The already-split path segments; none at all is no path, which `data_has` answers false for.
26
+ * @returns True when every segment names a key the value before it holds.
27
+ */
28
+ export declare function hasPluckPath(item: unknown, segments: readonly string[]): boolean;
29
+ /**
30
+ * Read one key of a target the way `data_get` steps into it, telling a key that holds null from a missing one.
31
+ *
32
+ * @param target - The value to read the key from.
33
+ * @param segment - The key, read as one literal segment.
34
+ * @returns `[true, value]` when the target holds the key, else `[false, undefined]`.
35
+ */
36
+ export declare function readPluckKey(target: unknown, segment: string): [boolean, unknown];
37
+ /**
38
+ * Split a pluck value or key argument into path segments the way Laravel's
39
+ * `explodePluckParameters` does: strings split on dots, arrays pass
40
+ * through, and `null` (the "keep the whole item" value form) yields no
41
+ * segments at all so {@linkcode resolvePluckPath} returns the item itself —
42
+ * `data_get($item, null)` short-circuits to `$target` before ever touching
43
+ * a segment loop, and zero segments has the same effect here.
44
+ *
45
+ * @param path - The path to split.
46
+ * @returns The path segments.
47
+ */
48
+ export declare function explodePluckPath(path: string | readonly string[] | null): string[];
49
+ /**
50
+ * Determine whether a value reads its entries through `offsetExists` and `offsetGet`, as PHP's ArrayAccess does.
51
+ *
52
+ * @param value - The value to test.
53
+ * @returns True for an object other than a plain one with both methods; a plain object is data.
54
+ */
55
+ export declare function isArrayAccess(value: unknown): value is {
56
+ offsetExists(offset: string): unknown;
57
+ offsetGet(offset: string): unknown;
58
+ };
59
+ /**
60
+ * Determine whether a value hands out its items through `all()`, as a PHP Enumerable such as a Collection does.
61
+ *
62
+ * @param value - The value to test.
63
+ * @returns True for an object other than a plain one with an `all` method; a plain object is data.
64
+ */
65
+ export declare function isEnumerable(value: unknown): value is {
66
+ all(): unknown;
67
+ };
68
+ //# sourceMappingURL=pluck.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pluck.d.ts","sourceRoot":"","sources":["../src/pluck.ts"],"names":[],"mappings":"AAkBA;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CAClC,MAAM,EACA,OAAO,EAAE,GACT,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,GAC5B,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,GACpC,OAAO,EAAE,CAWX;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC5B,IAAI,EAAE,OAAO,EACb,QAAQ,EAAE,SAAS,MAAM,EAAE,GAC5B,OAAO,CA2BT;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CACxB,IAAI,EAAE,OAAO,EACb,QAAQ,EAAE,SAAS,MAAM,EAAE,GAC5B,OAAO,CAgBT;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CACxB,MAAM,EAAE,OAAO,EACf,OAAO,EAAE,MAAM,GAChB,CAAC,OAAO,EAAE,OAAO,CAAC,CAIpB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC5B,IAAI,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,GAAG,IAAI,GACxC,MAAM,EAAE,CAUV;AA0CD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IACpD,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IACtC,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;CACtC,CAOA;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,GAAG,IAAI,OAAO,CAAA;CAAE,CAExE"}
package/dist/pluck.js ADDED
@@ -0,0 +1,62 @@
1
+ import { isNull as p, isArray as s, isMap as a, keyedEntries as x, isObject as f, isPlainObject as b, isFunction as l, isUndefined as h, phpArrayKey as O, isNumber as k, readMapKey as j, isPhpFalsy as A } from "@tolki/utils";
2
+ const u = /* @__PURE__ */ Symbol("absent");
3
+ function E(n) {
4
+ return s(n) ? n : a(n) ? x(n).map(([, r]) => r) : Object.values(n);
5
+ }
6
+ function d(n, r) {
7
+ if (r.length === 0)
8
+ return n;
9
+ const [i, ...e] = r;
10
+ if (i === "*") {
11
+ const c = y(n) ? n.all() : n;
12
+ return !s(c) && !f(c) ? null : E(c).map(
13
+ (P) => d(P, e)
14
+ );
15
+ }
16
+ const o = t(n, i);
17
+ return o === u || h(o) ? null : d(o, e);
18
+ }
19
+ function w(n, r) {
20
+ if (r.length === 0)
21
+ return !1;
22
+ let i = n;
23
+ for (const e of r)
24
+ if (i = t(i, e), i === u)
25
+ return !1;
26
+ return !0;
27
+ }
28
+ function F(n, r) {
29
+ const i = t(n, r);
30
+ return i === u ? [!1, void 0] : [!0, i];
31
+ }
32
+ function G(n) {
33
+ return p(n) ? [] : s(n) ? [...n] : String(n).split(".");
34
+ }
35
+ function t(n, r) {
36
+ if (s(n)) {
37
+ const i = O(r);
38
+ return k(i) && Object.hasOwn(n, i) ? n[i] : u;
39
+ }
40
+ if (!f(n))
41
+ return u;
42
+ if (a(n)) {
43
+ const i = j(n, r);
44
+ return i.found ? i.value : u;
45
+ }
46
+ return K(n) ? (y(n) ? t(n.all(), r) !== u : !A(n.offsetExists(r))) ? n.offsetGet(r) : u : Object.hasOwn(n, r) ? n[r] : u;
47
+ }
48
+ function K(n) {
49
+ return f(n) && !b(n) && l(n.offsetExists) && l(n.offsetGet);
50
+ }
51
+ function y(n) {
52
+ return f(n) && !b(n) && l(n.all);
53
+ }
54
+ export {
55
+ G as explodePluckPath,
56
+ E as getPluckWildcardValues,
57
+ w as hasPluckPath,
58
+ K as isArrayAccess,
59
+ y as isEnumerable,
60
+ F as readPluckKey,
61
+ d as resolvePluckPath
62
+ };
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@tolki/path",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "description": "Utilities for working with path-based operations across arrays/objects.",
5
+ "author": "Abraham Arango <https://github.com/abetwothree>",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/abetwothree/tolki.git",
10
+ "directory": "packages/path"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/abetwothree/tolki/issues"
14
+ },
15
+ "type": "module",
16
+ "main": "dist/index.js",
17
+ "keywords": [
18
+ "path",
19
+ "array",
20
+ "object",
21
+ "utilities",
22
+ "dot-notation"
23
+ ],
24
+ "dependencies": {
25
+ "@tolki/utils": "^1.6.0",
26
+ "@tolki/types": "^1.9.0"
27
+ },
28
+ "sideEffects": false,
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "scripts": {
33
+ "build": "vite build"
34
+ },
35
+ "types": "dist/index.d.ts",
36
+ "exports": {
37
+ ".": {
38
+ "import": "./dist/index.js",
39
+ "require": "./dist/index.js",
40
+ "types": "./dist/index.d.ts"
41
+ },
42
+ "./path": {
43
+ "import": "./dist/path.js",
44
+ "types": "./dist/path.d.ts"
45
+ },
46
+ "./pluck": {
47
+ "import": "./dist/pluck.js",
48
+ "types": "./dist/pluck.d.ts"
49
+ }
50
+ }
6
51
  }