UNPKG

expand-value

Version:

Get deeply nested values from an object, like dot-prop and get-value, but with support for advanced features like bracket-notation and more.

1 lines 7.32 kB
{"version":3,"sources":["../../../src/utils.ts","../../../src/nodes/Node.ts","../../../src/nodes/Block.ts","../../../src/nodes/Location.ts"],"sourcesContent":["import isNumber from 'is-number';\nimport { Segmenter } from 'intl-segmenter'; // optimized version of Intl.Segmenter\nexport { isNumber };\n\nconst { defineProperty } = Reflect;\n\nexport const isObject = val => val && typeof val === 'object' && !Array.isArray(val);\n\nexport const unquote = str => {\n if (!str) return '';\n return str.replace(/^['\"`]|['\"`]$/g, '');\n};\n\nexport const define = (node, key, value) => {\n defineProperty(node, key, {\n configurable: true,\n enumerable: false,\n writable: true,\n value\n });\n};\n\nexport const size = value => {\n if (value == null) return 0;\n if (isNumber(value)) return String(value).length;\n if (isObject(value)) return Object.keys(value).length;\n if (typeof value.length === 'number') return value.length;\n if (typeof value.size === 'number') return value.size;\n return null;\n};\n\nexport const isValidObject = val => {\n return isObject(val) || Array.isArray(val) || typeof val === 'function';\n};\n\nexport const isSafeKey = key => {\n return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';\n};\n\nexport const isValid = (key, data, options) => {\n if (!isSafeKey(key)) {\n return false;\n }\n\n if (typeof options.isValid === 'function') {\n return options.isValid(key, data);\n }\n\n return true;\n};\n\nexport const isArrayLike = obj => {\n return obj != null && typeof obj === 'object' && typeof obj.length === 'number';\n};\n\nexport const toKey = value => {\n if (typeof value === 'symbol') return value;\n if (value === 0 && 1 / value === -Infinity) return '-0';\n return String(value);\n};\n\nexport const findSafeBreakPoint = input => {\n // Work backwards from the end of the input\n for (let i = input.length - 1; i >= 0; i--) {\n // Check for whitespace or simple ASCII characters\n if (/\\s/.test(input[i]) || /^[\\x20-\\x7E]$/.test(input[i])) {\n return i + 1;\n }\n }\n\n // If no safe break points were found, return the full length\n return input.length;\n};\n\nexport const getSegments = (input, language = 'en', granularity) => {\n const segmenter = new Segmenter(language, { granularity, localeMatcher: 'best fit' });\n return Array.from(segmenter.segment(input)).map(segment => segment.segment);\n};\n\nexport const getGraphemes = (input, language = 'en', maxChunkLength = 500) => {\n const graphemes = [];\n let position = 0;\n\n while (position < input.length) {\n const remainingText = input.slice(position);\n const chunkSize = Math.min(maxChunkLength, remainingText.length);\n const potentialChunk = remainingText.slice(0, chunkSize);\n\n // Find a safe position to break the chunk\n const breakPoint = findSafeBreakPoint(potentialChunk);\n const chunk = potentialChunk.slice(0, breakPoint);\n\n // Process the chunk with Intl.Segmenter\n const chunkSegments = getSegments(chunk, language, 'grapheme');\n graphemes.push(...chunkSegments);\n\n position += breakPoint;\n }\n\n return graphemes;\n};\n","import { define } from '~/utils';\n\nexport class Node {\n type: string;\n value: string;\n output?: string;\n symbol?: string;\n parent?: { nodes: Node[] };\n\n constructor(node: {\n type: string;\n value?: string;\n output?: string;\n symbol?: string;\n alt?: unknown;\n match?: unknown;\n loc?: unknown;\n }) {\n this.type = node.type;\n this.value = node.value || '';\n\n if (node.output != null && node.output !== '') {\n this.output = node.output;\n }\n\n if (node.symbol) {\n this.symbol = node.symbol;\n }\n\n define(this, 'alt', node.alt);\n define(this, 'match', node.match);\n define(this, 'loc', node.loc);\n }\n\n get siblings(): Node[] {\n return this.parent?.nodes || [];\n }\n}\n\nexport default Node;\n","import { define } from '~/utils';\nimport { Node } from './Node';\n\nexport class Block extends Node {\n nodes: unknown[];\n\n constructor(node: unknown) {\n super(node);\n this.nodes = node.nodes || [];\n }\n\n append(input: string): void {\n // eslint-disable-next-line\n this.parent && this.parent.append(input);\n this.output = this.output || '';\n this.output += input;\n }\n\n push(node: unknown): void {\n define(node, 'parent', this);\n this.nodes.push(node);\n }\n}\n\nexport default Block;\n","export class Position {\n index: number;\n line: number;\n col: number;\n\n constructor(loc: { index: number; line: number; col: number }) {\n this.index = loc.index;\n this.line = loc.line;\n this.col = loc.col;\n }\n}\n\nexport class Location {\n start: Position;\n end: Position;\n\n constructor(start: Position, end: Position) {\n this.start = start;\n this.end = end;\n }\n\n slice(input: string): string {\n return input.slice(...this.range);\n }\n\n get range(): [number, number] {\n return [this.start.index, this.end.index];\n }\n\n get lines(): [number, number] {\n return [this.start.line, this.end.line];\n }\n}\n\nexport const location = (loc: { index: number; line: number; col: number }) => {\n const start = new Position(loc);\n\n return (node: unknown) => {\n node.loc = new Location(start, new Position(loc));\n return node;\n };\n};\n\nlocation.Position = Position;\nlocation.Location = Location;\nlocation.location = location;\n\nexport default Location;\n"],"mappings":";;;;AAAA,OAAO,cAAc;AACrB,SAAS,iBAAiB;AAG1B,IAAM,EAAE,eAAe,IAAI;AASpB,IAAM,SAAS,wBAAC,MAAM,KAAK,UAAU;AAC1C,iBAAe,MAAM,KAAK;AAAA,IACxB,cAAc;AAAA,IACd,YAAY;AAAA,IACZ,UAAU;AAAA,IACV;AAAA,EACF,CAAC;AACH,GAPsB;;;ACXf,IAAM,OAAN,MAAW;AAAA,EAFlB,OAEkB;AAAA;AAAA;AAAA,EAChB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA,YAAY,MAQT;AACD,SAAK,OAAO,KAAK;AACjB,SAAK,QAAQ,KAAK,SAAS;AAE3B,QAAI,KAAK,UAAU,QAAQ,KAAK,WAAW,IAAI;AAC7C,WAAK,SAAS,KAAK;AAAA,IACrB;AAEA,QAAI,KAAK,QAAQ;AACf,WAAK,SAAS,KAAK;AAAA,IACrB;AAEA,WAAO,MAAM,OAAO,KAAK,GAAG;AAC5B,WAAO,MAAM,SAAS,KAAK,KAAK;AAChC,WAAO,MAAM,OAAO,KAAK,GAAG;AAAA,EAC9B;AAAA,EAEA,IAAI,WAAmB;AACrB,WAAO,KAAK,QAAQ,SAAS,CAAC;AAAA,EAChC;AACF;;;AClCO,IAAM,QAAN,cAAoB,KAAK;AAAA,EAHhC,OAGgC;AAAA;AAAA;AAAA,EAC9B;AAAA,EAEA,YAAY,MAAe;AACzB,UAAM,IAAI;AACV,SAAK,QAAQ,KAAK,SAAS,CAAC;AAAA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