A library to allow recursion of nestable javascript objects using available prototypial methods
npm install recursionjsrecursionjsA library to allow flexible recursion of javascript objects, written in Typescript.
Doing nested recursion on Javascript objects is a pain.
It's hard to compare them all, they're all slightly different when accessing properties and reading them.
To provide familiar Javascript prototype methods to use in a recursive fashion, with inter-object compatibility.
This in development, very early stages. I want to do as many methods as possible, but if you want something prioritised just post an issue.
Anything I've written below is available and works.
Easy, just parse your object, array, map or set through the recursion() function:
``tsx
import { recursion } from 'recursionjs'
// OR
import recursion from 'recursionjs'
const myOldObject = { name: 'shrek', wife: 'fiona' }
const myNewObject = recursion(myOldObject)
// Now you can access recursionjs methods
// callbacks hold two objects:
// + 1st object is the original arguments, available to deconstruct ,
// + 2nd object holds handy methods for getting values handy in recursion (parents, depth, path)
const hasShrek = myNewObject.some(({ key, value }, { depth, parent, path }) => {
return key === 'name' && value === 'shrek'
})
`
Outputs a boolean based off the callback:
`tsx
const myNewObject = recursion({ name: 'shrek', wife: 'fiona' })
// returns true
const hasShrek = myNewObject.some(({ key, value }) => {
return key === 'name' && value === 'shrek'
})
`
An alias for recursion.mapValues()
This maps the value of the Object.
`tsx
const myNewObject = recursion({ name: 'shrek', wife: 'fiona' })
// returns { shrek: 'name', fiona: 'wife' }
const hasShrek = myNewObject.some(({ value }) => value)
`
Filters the array.
`tsx
const myNewObject = recursion({ name: 'shrek', wife: 'fiona' })
// returns { shrek: 'name'}
const hasShrek = myNewObject.filter(({ value }) => value === 'shrek')
`
I'd love to hear from anyone looking to benefit from something like this.
It's early days and if you want a feature or have any ideas you'd like to jot down, just ask as an issue :)
I plan to add as much as I can in as it's not hard once the architecture of the library is understood.
In a nutshell, each object gets converted to the object that the prototype method requires and then runs the method, which runs on it's value property again if value instanceof Object.
For example we'll use recursion.some(). This comes from and uses Array.prototype.some() under the hood.
We must convert from our input to an array of tuples/entries). now we have a standardized array, we run the function against it.
We have the public facing function (some) and the private facing function (_some())._some()
The public function calls using the input parsed to recursion()` class.