Skip to content

Conversation

@borodyadka
Copy link

Function that will check for should value be flattened or not.

Example:

const SCHEMA = {
    key1: {
        foo: 'foo',
        bar: 'bar'
    },
    key2: {
        $keep: true,
        test: 'test'
    }
};

let r = flat.flatten(SCHEMA, {
    check: (value) => {
        return !(value.$keep);
    }
});

console.log(JSON.stringify(r, null, 2));

Will print:

{
  "key1.foo": "foo",
  "key1.bar": "bar",
  "key2": {
    "$keep": true,
    "test": "test"
  }
}

I'm using it in my schema validator.

@borodyadka
Copy link
Author

ping @hughsk

@timoxley
Copy link
Contributor

@H8BIT I've suggested to @richardschneider in #36 that perhaps a mapKeys/mapValues option could a good solution for these types of problems… perhaps it also warrants filterKeys/filterValues?

For these types of things I usually use flat alongside split-object

e.g.

// note this is untested, just pseudo-code that might work
const filteredItems = split(flat(SCHEMA)).filter(({key, value}) => {
  return !value.$keep
})

const r = flat.unflatten(split.join(filteredItems))

@borodyadka borodyadka closed this Aug 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants