From b7debd668d03b2236cea1b0291acb8efc6f3ee0d Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 16 Feb 2024 16:11:13 -0400 Subject: data-checks, sugar: factor out getNestedProp --- src/data/checks.js | 9 +-------- src/util/sugar.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/data/checks.js b/src/data/checks.js index ad86087b..96f4cd8c 100644 --- a/src/data/checks.js +++ b/src/data/checks.js @@ -4,7 +4,7 @@ import {inspect as nodeInspect} from 'node:util'; import {colors, ENABLE_COLOR} from '#cli'; import CacheableObject from '#cacheable-object'; -import {compareArrays, empty} from '#sugar'; +import {compareArrays, empty, getNestedProp} from '#sugar'; import Thing from '#thing'; import thingConstructors from '#things'; import {commentaryRegexCaseSensitive} from '#wiki-data'; @@ -167,13 +167,6 @@ export function filterReferenceErrors(wikiData, { }], ]; - function getNestedProp(obj, key) { - const recursive = (o, k) => - k.length === 1 ? o[k[0]] : recursive(o[k[0]], k.slice(1)); - const keys = key.split(/(?<=(? { diff --git a/src/util/sugar.js b/src/util/sugar.js index 58a2c3e9..3c364ee5 100644 --- a/src/util/sugar.js +++ b/src/util/sugar.js @@ -263,11 +263,22 @@ export function delay(ms) { // Stolen from here: https://stackoverflow.com/a/3561711 // // There's a proposal for a native JS function like this, 8ut it's not even -// past stage 1 yet: https://github.com/tc39/proposal-regex-escaping +// past stage ~~1~~ 2 yet: https://github.com/tc39/proposal-regex-escaping export function escapeRegex(string) { return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); } +// Follows a key path like 'foo.bar.baz' to get an item nested deeply inside +// an object. +export function getNestedProp(obj, key) { + const recursive = (o, k) => + (k.length === 1 + ? o[k[0]] + : recursive(o[k[0]], k.slice(1))); + + return recursive(obj, key.split(/(?<=(?