Skip to content

Commit

Permalink
add special case for contents and collections keys, for now
Browse files Browse the repository at this point in the history
  • Loading branch information
rsek committed Jan 17, 2024
1 parent 360df52 commit 7276776
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import CONST from '../IdElements/CONST.js';
declare class ObjectGlobber<TTuple extends Array<PropertyKey> = Array<PropertyKey>> extends Array<TTuple[number]> {
#private;
constructor(...items: TTuple);
/** Keys that are part of the real object path, but not part of the ID */
static readonly implicitKeys: string[];
/** Does this path contain any wildcard or globstar elements? */
get wildcard(): boolean;
toJSON(): unknown[];
Expand All @@ -29,7 +31,7 @@ declare class ObjectGlobber<TTuple extends Array<PropertyKey> = Array<PropertyKe
matchTest?: ObjectGlobber.MatchTest;
includeArrays: boolean;
}): unknown[];
static getKeyMatches(from: object, matchKey: PropertyKey, { forEachMatch, matchTest, includeArrays }?: {
static getKeyMatches(from: object, matchedKey: PropertyKey, { forEachMatch, matchTest, includeArrays }?: {
forEachMatch?: ObjectGlobber.WalkIteratee;
matchTest?: ObjectGlobber.MatchTest;
includeArrays: boolean;
Expand Down
32 changes: 19 additions & 13 deletions pkg/nodejs/@datasworn/core/dist/ObjectGlobPath/ObjectGlobber.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ class ObjectGlobber extends Array {
const nextPath = keys.slice(1);
// console.log('next:', nextKey, nextPath)
if (nextPath.length === 0)
return _a.getKeyMatches(from, nextKey, { includeArrays, matchTest });
return _a.getKeyMatches(from, nextKey, {
includeArrays,
matchTest
});
const matches = _a.getKeyMatches(from, nextKey, {
includeArrays
});
Expand All @@ -121,27 +124,27 @@ class ObjectGlobber extends Array {
}
return results;
}
static getKeyMatches(from, matchKey, { forEachMatch, matchTest, includeArrays = false } = {
static getKeyMatches(from, matchedKey, { forEachMatch, matchTest, includeArrays = false } = {
includeArrays: false
}) {
if (!_a.isPropertyKey(matchKey))
throw new Error(`Expected a number, string, or symbol key, but got ${typeof matchKey}`);
if (!_a.isPropertyKey(matchedKey))
throw new Error(`Expected a number, string, or symbol key, but got ${typeof matchedKey}`);
const results = [];
const hasMatchTest = typeof matchTest === 'function';
function iterateWildcardMatch(key, value, results) {
if (!hasMatchTest || matchTest(value, key, matchKey))
if (!hasMatchTest || matchTest(value, key, matchedKey))
results.push(value);
}
function iterateGlobstarMatch(key, value, results) {
iterateWildcardMatch(key, value, results);
if (_a.isWalkable(value, includeArrays)) {
results.push(..._a.getKeyMatches(value, matchKey, {
results.push(..._a.getKeyMatches(value, matchedKey, {
matchTest,
includeArrays
}));
}
}
switch (matchKey) {
switch (matchedKey) {
case _a.WILDCARD.description:
case _a.WILDCARD:
if (from instanceof Map) {
Expand Down Expand Up @@ -174,13 +177,14 @@ class ObjectGlobber extends Array {
default: {
let value;
if (from instanceof Map)
value = from.get(matchKey);
value = from.get(matchedKey);
else
value = from[matchKey];
if (typeof value === 'undefined')
throw new Error(`Unable to find key ${typeof matchKey === 'symbol'
? matchKey.toString()
: JSON.stringify(matchKey)}`);
value = from[matchedKey];
if (typeof value === 'undefined' &&
!_a.implicitKeys.includes(matchedKey))
throw new Error(`Unable to find key ${typeof matchedKey === 'symbol'
? matchedKey.toString()
: JSON.stringify(matchedKey)}`);
results.push(value);
break;
}
Expand Down Expand Up @@ -289,6 +293,8 @@ _a = ObjectGlobber, _ObjectGlobber_getPlainObjectPaths = function _ObjectGlobber
}
return results;
};
/** Keys that are part of the real object path, but not part of the ID */
ObjectGlobber.implicitKeys = ['contents', 'collections'];
(function (ObjectGlobber) {
/** Represents a glob wildcard path element, usually expressed as `*` */
ObjectGlobber.WILDCARD = Symbol(CONST_js_1.default.WildcardString);
Expand Down
36 changes: 23 additions & 13 deletions src/pkg-core/ObjectGlobPath/ObjectGlobber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class ObjectGlobber<
super(...(items.map(ObjectGlobber.replaceGlobString as any) as any))
}

/** Keys that are part of the real object path, but not part of the ID */
static readonly implicitKeys = ['contents', 'collections']

/** Does this path contain any wildcard or globstar elements? */
get wildcard() {
return this.some(ObjectGlobber.isGlobElement)
Expand Down Expand Up @@ -150,7 +153,10 @@ class ObjectGlobber<
// console.log('next:', nextKey, nextPath)

if (nextPath.length === 0)
return ObjectGlobber.getKeyMatches(from, nextKey, { includeArrays, matchTest })
return ObjectGlobber.getKeyMatches(from, nextKey, {
includeArrays,
matchTest
})

const matches = ObjectGlobber.getKeyMatches(from, nextKey, {
includeArrays
Expand All @@ -172,7 +178,7 @@ class ObjectGlobber<

static getKeyMatches(
from: object,
matchKey: PropertyKey,
matchedKey: PropertyKey,
{
forEachMatch,
matchTest,
Expand All @@ -185,9 +191,9 @@ class ObjectGlobber<
includeArrays: false
}
): unknown[] {
if (!ObjectGlobber.isPropertyKey(matchKey))
if (!ObjectGlobber.isPropertyKey(matchedKey))
throw new Error(
`Expected a number, string, or symbol key, but got ${typeof matchKey}`
`Expected a number, string, or symbol key, but got ${typeof matchedKey}`
)

const results: unknown[] = []
Expand All @@ -199,7 +205,8 @@ class ObjectGlobber<
value: unknown,
results: unknown[]
) {
if (!hasMatchTest || matchTest(value, key, matchKey)) results.push(value)
if (!hasMatchTest || matchTest(value, key, matchedKey))
results.push(value)
}
function iterateGlobstarMatch(
key: PropertyKey,
Expand All @@ -209,15 +216,15 @@ class ObjectGlobber<
iterateWildcardMatch(key, value, results)
if (ObjectGlobber.isWalkable(value, includeArrays)) {
results.push(
...ObjectGlobber.getKeyMatches(value, matchKey, {
...ObjectGlobber.getKeyMatches(value, matchedKey, {
matchTest,
includeArrays
})
)
}
}

switch (matchKey) {
switch (matchedKey) {
case ObjectGlobber.WILDCARD.description:
case ObjectGlobber.WILDCARD:
if (from instanceof Map) {
Expand Down Expand Up @@ -245,15 +252,18 @@ class ObjectGlobber<
break
default: {
let value: unknown
if (from instanceof Map) value = from.get(matchKey)
else value = from[matchKey]
if (from instanceof Map) value = from.get(matchedKey)
else value = from[matchedKey]

if (typeof value === 'undefined')
if (
typeof value === 'undefined' &&
!ObjectGlobber.implicitKeys.includes(matchedKey as string)
)
throw new Error(
`Unable to find key ${
typeof matchKey === 'symbol'
? (matchKey as any).toString()
: JSON.stringify(matchKey)
typeof matchedKey === 'symbol'
? (matchedKey as any).toString()
: JSON.stringify(matchedKey)
}`
)

Expand Down

0 comments on commit 7276776

Please sign in to comment.