0.3.1 • Published 5 years ago

recursionjs v0.3.1

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

recursionjs

A library to allow flexible recursion of javascript objects, written in Typescript.

Motivation

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.

Goal

To provide familiar Javascript prototype methods to use in a recursive fashion, with inter-object compatibility.

Note

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.

How to use?

Easy, just parse your object, array, map or set through the recursion() function:

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'
})

API

array.some()

Outputs a boolean based off the callback:

const myNewObject = recursion({ name: 'shrek', wife: 'fiona' })

// returns true
const hasShrek = myNewObject.some(({ key, value }) => {
  return key === 'name' && value === 'shrek'
})

array.map()

An alias for recursion.mapValues()

This maps the value of the Object.

const myNewObject = recursion({ name: 'shrek', wife: 'fiona' })

// returns { shrek: 'name', fiona: 'wife' }
const hasShrek = myNewObject.some(({ value }) => value)

array.filter()

Filters the array.

const myNewObject = recursion({ name: 'shrek', wife: 'fiona' })

// returns { shrek: 'name'}
const hasShrek = myNewObject.filter(({ value }) => value === 'shrek')

Contribution and Discussion

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.

Architecture

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()). The public function calls _some() using the input parsed to recursion() class.

0.3.1

5 years ago

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago