npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

edata

v0.9.9

Published

edata is nested observable reactive EventEmitter with .value getter/setter and lodash style path.

Downloads

34

Readme

EDATA (Enhanced DATA)

edata is the nested observable reactive EventEmitter with .value getter/setter, lodash style path, and keep Observer Pattern in mind.

It roughly referenced Object.observe API, but instead using getter/setter to wrap object, lightweight than Proxy.

Build Status NPM Version

Install

NPM

npm install --save edata
import edata from 'edata'

Usage

- Quick Start

Below can give you a quick idea of edata:

var root = edata({a: {b: {c: {}}}})
// plain_object ---> edata

Complete example:

import edata from 'edata'
const root = edata({
    age: 20,
    firstName: 'Hello',
    lastName: 'World',
    address: {
        city: 'Earth'
    }
})
const callback = ({type, path}) => console.log(`--> ${type}: ${path}`)
root.watch(callback)
root.set('address.city', 'Moon') // LOG: --> update: ['address', 'city']
root.get('address.city').value // Moon

Plain object wrapped into edata:

edata = new EventEmitter(object)

so use edata.on can watch changes, use edata.value(getter/setter) to get nested edata.

root.value.firstName.value  // get -> firstName
root.value.firstName.value = 'name'  // set -> firstName
root.value.address.value.city.on('change', callback)  // watch on 'change'

Can also use lodash style:

root.get('firstName').value  // get: firstName
root.set('firstName', 'name')  // set: firstName
root.get('address.city').on('change', ...) // watch change

Proxy usage:

use .proxy() to shorten the path:

const data = root.proxy()
data.address.city // Moon
data.__edata__  // get back the `root` edata

- Watch changes

Any edata can watch data changes inside(any level):

const onDataChange = ({data, type, path})=>{
    console.log('value mutated:', path, type, data.unwrap())
}
const unwatch = root.watch('change', onDataChange)

root.set('address.city', 'Mars')
// [LOG] data mutated: [ 'address', 'city' ] add Mars
unwatch() // stop watch
root.get('address.city').value = 'Earth'
// nothing outout

Watch single data change event:

root.get('firstName').on('change', ({path, type, data})=>{
    console.log(path, type, 'to: ' + data)
})
root.set('firstName', 'Hi')
//[LOG] firstName update to: Hi

Note: edata.on('change', callback) has shortcut: edata.map(callback)

- Define Data Relations

You can define data relations using setComputed, as below:

const root = edata({
  firstName: 'Hello',
  lastName: 'World'
})
root.setComputed(
  'fullName',
  ['firstName', 'lastName'],
  ([firstName, lastName]) => firstName + ' ' + lastName
)
assert.equal(root.unwrap('fullName'), 'Hello World')
root.set('firstName', 'Green')
assert.equal(root.unwrap('fullName'), 'Green World')

- Flat data

Since the edata objects nested as EventEmitter, you can flat this structure into plain object using edata.unwrap():

root.unwrap()
// flat root: {age: 20, firstName: 'Hello', lastName: 'World', address: {city: 'Earth'}}

root.unwrap('address')
// flat address: {city: 'Earth'}

root.unset('address')
// delete address

root.unwrap()
// {age: 20, firstName: 'Hello', lastName: 'World'}

Also exist edata.toJSON(), so the JSON.stringify(edata) just like

JSON.stringify(edata.unwrap({json:true}))

- Use in React

The pros is you can use edata.set() instead of this.setState:

import edata from 'edata'
const root = edata({user: {name: 'earth'}})

class App extends React.Component {
    constructor(props){
        super(props)
        const {model} = this.props
        this.state = model.unwrap()
        // state: {name: 'earth'}
        model.watch(()=>{
            this.setState(model.unwrap())
        })
    }
    
    render(){
        const {model} = this.props
        const name = model.unwrap('name')
        return <div>
            <h3>Name: {name}</h3>
            <input
              value={name}
              onChange={e=>{model.set('name', e.target.value)}}
            />
        </div>
    }
}

ReactDOM.render(<App edata={root.cut('user')} />, app)

You can play with the demo here

API

See API Document