softkave-js-utils
Version:
JavaScript & Typescript utility functions, types, and classes
81 lines (80 loc) • 2.15 kB
TypeScript
import { Primitive } from 'type-fest';
import { AnyObject } from '../types.js';
type Key = Array<string | number>;
type KeyType = Array<'string' | 'number'>;
type Value = Primitive;
type IndexedJson = Record<string, {
key: Key;
value: Value[];
keyType: KeyType;
valueType: Set<string>;
}>;
export interface IIndexJsonOptions {
/**
* If true, values sub-indexed within numeric keys will be attributed also to
* the parent key.
*
* @example
* ```ts
* const json = {
* a: [
* {
* b: 1,
* },
* {
* b: 2,
* },
* ],
* }
*
* indexJson(json, undefined, undefined, {flattenNumericKeys: true})
*
* // Result:
* // {
* // 'a.0.b': {
* // key: ['a', '0', 'b'],
* // value: [1],
* // keyType: ['string', 'number', 'string'],
* // valueType: new Set(['number']),
* // },
* // 'a.1.b': {
* // key: ['a', '1', 'b'],
* // value: [2],
* // keyType: ['string', 'number', 'string'],
* // valueType: new Set(['number']),
* // },
* // "a.b": {
* // key: ['a', 'b'],
* // value: [1, 2],
* // keyType: ['string', 'string'],
* // valueType: new Set(['number']),
* // },
* // }
*
* indexJson(json, undefined, undefined, {flattenNumericKeys: false})
*
* // Result:
* // {
* // 'a.0.b': {
* // key: ['a', '0', 'b'],
* // value: [1],
* // keyType: ['string', 'number', 'string'],
* // valueType: new Set(['number']),
* // },
* // 'a.1.b': {
* // key: ['a', '1', 'b'],
* // value: [2],
* // keyType: ['string', 'number', 'string'],
* // valueType: new Set(['number']),
* // },
* // }
* ```
* @default false
*/
flattenNumericKeys?: boolean;
}
export declare function indexJson(json: AnyObject, options?: IIndexJsonOptions, __internal?: {
parentKeyList: Array<Key>;
indexed: IndexedJson;
}): IndexedJson;
export {};