1.0.2 • Published 1 year ago

@juigorg/odio-repellat v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@juigorg/odio-repellat Build Status

Take a nested Javascript object and @juigorg/odio-repellatten it, or un@juigorg/odio-repellatten an object with delimited keys.

Installation

$ npm install @juigorg/odio-repellat

Methods

@juigorg/odio-repellatten(original, options)

Flattens the object - it'll return an object one level deep, regardless of how nested the original object was:

import { @juigorg/odio-repellatten } from '@juigorg/odio-repellat'

@juigorg/odio-repellatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
})

// {
//   'key1.keyA': 'valueI',
//   'key2.keyB': 'valueII',
//   'key3.a.b.c': 2
// }

un@juigorg/odio-repellatten(original, options)

Flattening is reversible too, you can call un@juigorg/odio-repellatten on an object:

import { un@juigorg/odio-repellatten } from '@juigorg/odio-repellat'

un@juigorg/odio-repellatten({
    'three.levels.deep': 42,
    'three.levels': {
        nested: true
    }
})

// {
//     three: {
//         levels: {
//             deep: 42,
//             nested: true
//         }
//     }
// }

Options

delimiter

Use a custom delimiter for (un)@juigorg/odio-repellattening your objects, instead of ..

safe

When enabled, both @juigorg/odio-repellat and un@juigorg/odio-repellatten will preserve arrays and their contents. This is disabled by default.

import { @juigorg/odio-repellatten } from '@juigorg/odio-repellat'

@juigorg/odio-repellatten({
    this: [
        { contains: 'arrays' },
        { preserving: {
              them: 'for you'
        }}
    ]
}, {
    safe: true
})

// {
//     'this': [
//         { contains: 'arrays' },
//         { preserving: {
//             them: 'for you'
//         }}
//     ]
// }

object

When enabled, arrays will not be created automatically when calling un@juigorg/odio-repellatten, like so:

un@juigorg/odio-repellatten({
    'hello.you.0': 'ipsum',
    'hello.you.1': 'lorem',
    'hello.other.world': 'foo'
}, { object: true })

// hello: {
//     you: {
//         0: 'ipsum',
//         1: 'lorem',
//     },
//     other: { world: 'foo' }
// }

overwrite

When enabled, existing keys in the un@juigorg/odio-repellattened object may be overwritten if they cannot hold a newly encountered nested value:

un@juigorg/odio-repellatten({
    'TRAVIS': 'true',
    'TRAVIS.DIR': '/home/travis/build/kvz/environmental'
}, { overwrite: true })

// TRAVIS: {
//     DIR: '/home/travis/build/kvz/environmental'
// }

Without overwrite set to true, the TRAVIS key would already have been set to a string, thus could not accept the nested DIR element.

This only makes sense on ordered arrays, and since we're overwriting data, should be used with care.

maxDepth

Maximum number of nested objects to @juigorg/odio-repellatten.

import { @juigorg/odio-repellatten } from '@juigorg/odio-repellat'

@juigorg/odio-repellatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
}, { maxDepth: 2 })

// {
//   'key1.keyA': 'valueI',
//   'key2.keyB': 'valueII',
//   'key3.a': { b: { c: 2 } }
// }

transformKey

Transform each part of a @juigorg/odio-repellat key before and after @juigorg/odio-repellattening.

import { @juigorg/odio-repellatten, un@juigorg/odio-repellatten } from '@juigorg/odio-repellat'

@juigorg/odio-repellatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
}, {
    transformKey: function(key){
      return '__' + key + '__';
    }
})

// {
//   '__key1__.__keyA__': 'valueI',
//   '__key2__.__keyB__': 'valueII',
//   '__key3__.__a__.__b__.__c__': 2
// }

un@juigorg/odio-repellatten({
      '__key1__.__keyA__': 'valueI',
      '__key2__.__keyB__': 'valueII',
      '__key3__.__a__.__b__.__c__': 2
}, {
    transformKey: function(key){
      return key.substring(2, key.length - 2)
    }
})

// {
//     key1: {
//         keyA: 'valueI'
//     },
//     key2: {
//         keyB: 'valueII'
//     },
//     key3: { a: { b: { c: 2 } } }
// }

Command Line Usage

@juigorg/odio-repellat is also available as a command line tool. You can run it with npx:

npx @juigorg/odio-repellat foo.json

Or install the @juigorg/odio-repellat command globally:

npm i -g @juigorg/odio-repellat && @juigorg/odio-repellat foo.json

Accepts a filename as an argument:

@juigorg/odio-repellat foo.json

Also accepts JSON on stdin:

cat foo.json | @juigorg/odio-repellat
YAMLbuffermapdeepconstSetObject.getPrototypeOfObject.assignfpsECMAScript 3reusequerystringchannelfastcopyes2017accessorzod_.extendcall-boundcircularjsxpureelasticachelocationprivateArray.prototype.includess3Streams-0styled-componentseslintconfiggroupeslinttypedutil.inspectansidom-testing-libraryflatPromiseestreevalidatedefinePropertyi18nesformatbeanstalkBigInt64Arrayhaskeysquotepinofinduppostcss-plugincssparentcallbindautoprefixerbundlerSymbol.toStringTagsnsjasmineacorndirgetPrototypeOfpropreducerdescriptortesterimportxtermgetOwnPropertyDescriptorhttpsjson-schema256trimES6cjkgesturesstylingReflect.getPrototypeOffind-upcheckjestresolveECMAScript 2019es2018touchshimIteratorimportexportbufferswritableECMAScriptglobal objectreworkbrowserslistasyncUint16Arrayfunction.lengthamazontimetrimLeftlinkswfperformancetyped arraytelephonespawnfiglethigher-orderstablexhrsharedarraybuffertypescriptcolorsvisualsymbolsvpctestingvaluesstringifyhashselfworkflowinspectawsFloat32ArrayInt32ArrayperformantassertvaluedeepcopyECMAScript 6electroncharacterstoSortedES2018bundlingES2022ObservablesRFC-6455fantasy-landschemeglobalsES5RegExp#flagsflagparseroutinguninstallbusybyteOffsetwafmakeassertionglobenderhasOwnPropertyomitlook-uplogappdirectoryArrayBuffer.prototype.sliceinternal slotqueuebrowserlistjsdiffiteratorlibphonenumbereveryargvexpresstestpolyfillreversedtoArrayArray.prototype.flattenintrinsices6private datacodesloggerObservablematchSymbolresttakerecursivecolourkeydotenvuser-streamstoReversedclientaccessibilityES2015waitspinnerses2015deepclonedeep-copyrapidUnderscoremanagerinstrumentationcollection.es63dmovefindLastconsumecallbackTypeScriptjson-schema-validationsyntaxexecfilearraysshrinkwrapchildfilterimmutablees-shim APIscheme-validationdragJSONramdaECMAScript 2018equalitythreenegative zeroArray.prototype.filtermkdirpes-shimsslotrmdirletespreecachehardlinksreadablestreamprototypecliagentfast-clonematchAllobjyupes2016l10nhttpenvironmentrfc4122Function.prototype.name$.extendprocessECMAScript 2016URLglobalThisfeedtc39rssmkdirsdeterministicArrayBuffer#sliceutilstapeprefixmapreduceinmobilesetlocalcolumninstallspeedAsyncIteratorprogressapiclassnamespnpm9ECMAScript 2023URLSearchParamsjsontrimStartbannerECMAScript 5idleprotocol-bufferseventDispatcherserializationES3jsreact-testing-librarysetPrototypeOfconcatisjson-schema-validatorsuperstructtoolkitclassesgenericsoncekoreansaferedirectchromespinnerESebsinstallerconfiggroupBynodeistanbuldiffentriesMicrosoftfunctionsjapanesetransportmodulesmodulesuperagentes8pyyamlECMAScript 2021requestregular-expressionhelpercopyec2consoleurlgraphqlairbnbprunegetintrinsicforEachnpmregexdomshebangemitroutewalkingcurriedfull-widthformattingutilpropertiesform-validationio-tskarmanpmignorewhatwgeffect-tssortedsameValueZerocommanderString.prototype.matchAllHyBilruCSSpicomatchpopmotionreadmkdirreducepatchtoStringTagvalidatoreslintpluginfixed-widthnamesredux-toolkitajvparserdescriptorsES2023source mapvars.env__proto__propertyspringmatchesjshintlockfiletransformes7routerES2020Object.keysdependency managerRegExp.prototype.flagsidframerdataViewfastifyspecassign
1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago