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

data-limbo

v0.3.6

Published

A data middleman serve Proxy between user (a UI component) and a data source (Vuex store, or just an object), retain and validate data before committing the changes to the data store

Downloads

19

Readme

js-data-limbo

This library provides a data middleman (a Proxy) of a data object (the 'principal' of the middleman) that behave as an intermediate layer of that data object. Any changes to the data object through the middleman is first retained in the middleman without affecting the data object. Until the '$commit()' method is called, the changes are committed to the actual data object. To discard the changes, simply discard the middleman object without $commit().

If any property of the data object that the middleman retains is an object (not a primitive), the middleman creates and retains a new middleman (Proxy) of that property. When "$commit()", the middleman commits all changes deep down.

This library relies on the support of Proxy object, an ES6 feature.

Creating the middleman

const DataLimbo = require('data-limbo')
...
var proxy = DataLimbo.middleman(dataObj)

Middleman property and methods

All middleman's own property and methods are prefixed with '$' to avoid conflict with the properties of the data object

$isProxy

Always 'true' to indicate the middleman is a proxy, not the actual object.

$principal

$principal of a middleman holds the actual object that the middleman represents

$changed

If any of the property of the middleman has been changed. "Change" here includes any create/update/delete of any property, and also includes any create/update/delete of any property of an object property (child) of the middleman (even if the child property itself is not updated).

When $changed is true, $commit() would actual make some changes to the middleman or its children.

$commit()

Commit the changes to the actual object, the $principal

$propertyState([propertyName])

If [propertyName] is not provided, and the middleman is being held by a parent middleman as holder of a property, returns the property state of the middleman (as a property of the parent middleman).

If [propertyName] is provided, return the property state of the property of the middleman.

A property state is:

  • RETAINED: retained property from the original data object without change
  • NEW: new property added to the middleman but still not committed
  • DELETED: property already deleted from the middleman but still not committed
  • DIRTY: property is updated since retained but still not committed

$testPrincipalEqual([propertyName])

Test if the property this middleman represents, when propertyName is not provided and this middleman is a property of a parent middleman, still equals (===) to the actual property of the actual data object.

Test if value of the property of this middleman with the propertyName still equals (===) to the actual property of the actual data object.

This method can be used to detect change to the actual data object that is not made through the middleman.

Examples

const DataLimbo = require('data-limbo')

// This is the actual data object
var dataObj = {one:1, two:"TWO", insideObj:{}}

// Create a middleman
var proxy = DataLimbo.middleman(dataObj)

// Check the middleman
console.log(proxy.one) // 1
console.log(proxy.$isProxy) // true
console.log(proxy.$principal === dataObj) // true
console.log(proxy.$changed) // false
console.log(proxy.$propertyState()) // undefined
console.log(proxy.$propertyState('one')) // RETAINED
console.log(proxy.insideObj.$propertyState()) // RETAINED
console.log(proxy.insideObj.$changed) // false
console.log(proxy.$propertyState("insideObj")) // RETAINED
console.log(proxy.$testPrincipalEqual()) // true
console.log(proxy.$testPrincipalEqual('one')) // true
console.log(proxy.insideObj.$testPrincipalEqual()) // true
console.log(JSON.stringify(proxy)) // {"one":1,"two":"TWO","insideObj":{}}

// make some change to a property of a child object
proxy.insideObj.answer=42
console.log(proxy.$changed) // true
console.log(proxy.insideObj.$changed) // true

// $testPrincipalEqual still returns true because the object is not changed
console.log(proxy.$testPrincipalEqual()) // true
console.log(proxy.insideObj.$testPrincipalEqual()) // true

// make more changes
proxy.one = 'ONE'
proxy.x = 'X'

// without $commit() the changes are only with the middleman
console.log(JSON.stringify(proxy)) // {"one":"ONE","two":"TWO","insideObj":{"answer":42},"x":"X"}
console.log(JSON.stringify(dataObj)) // {"one":1,"two":"TWO","insideObj":{}}

// check the middleman again
console.log(proxy.$propertyState()) // undefined
console.log(proxy.$changed) // true
console.log(proxy.$propertyState('one')) // DIRTY
console.log(proxy.$propertyState('x')) // NEW
console.log(proxy.insideObj.$propertyState()) // RETAINED
console.log(proxy.insideObj.$propertyState('answer')) // NEW
console.log(proxy.$propertyState("insideObj")) // RETAINED
console.log(proxy.$testPrincipalEqual('one')) // false

// commit, now the changes are made to the actual object
proxy.$commit()
console.log(JSON.stringify(proxy)) // {"one":"ONE","two":"TWO","insideObj":{"answer":42},"x":"X"}
console.log(JSON.stringify(dataObj)) // {"one":"ONE","two":"TWO","insideObj":{"answer":42},"x":"X"}

// check again after commit
console.log(proxy.$propertyState()) // undefined
console.log(proxy.$propertyState('one')) // RETAINED
console.log(proxy.$propertyState('x')) // RETAINED
console.log(proxy.insideObj.$propertyState()) // RETAINED
console.log(proxy.insideObj.$propertyState('answer')) // RETAINED
console.log(proxy.$propertyState("insideObj")) // RETAINED
console.log(proxy.$testPrincipalEqual()) // true
console.log(proxy.$testPrincipalEqual('one')) // true
console.log(proxy.insideObj.$testPrincipalEqual()) // true

// direct change to the actual object can be detected with $testPrincipalEqual, but not $changed property of the middleman
dataObj.one = '1'
console.log(proxy.$testPrincipalEqual('one')) // false
console.log(proxy.$changed) // false