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

pek

v0.3.5

Published

A modern data model for UI rendering

Downloads

8

Readme

Pek Logo

JavaScript data structures with immutable state events.

Install

npm install pek

Then:

const pek = require('pek');

Quick Start

To get started, create a data model for your application using the same data types as you would with JSON (String, Number, Boolean, Object, Array, and null):

const APP_STATE = {
  name: 'Todo List',
  lists: [
    {name: 'Shopping', items: ['Milk', 'Bananas']},
  ]
};

Next, create a Pēk model by passing this data model to pek():

const model = pek(APP_STATE); // ⇨ { name: 'Todo List', lists: [ { name: 'Shopping', items: [Object] } ] }

That's pretty much all there is to it. Your Pēk model looks and feels just like the original:

model.lists[0].name = 'Zoo Supplies';
model.lists[0].items.push('Monkeys');

model.lists[0]; // ⇨ { name: 'Zoo Supplies', items: [ 'Milk', 'Bananas', 'Monkeys' ] }

... with one important difference - you can subscribe to state changes:

const unsubscribe = model.__.on(state => console.log(state));

model.name = 'Pek Example';

Anytime your Pēk model changes, all listeners are notified. Moreover, the state listeners receive is immutable:

model.__.on(state => {
  state.name = 'Nopity nope nope';
  state.name; // ⇨ 'Pek Example 2'
});

model.name = '\Pek Example 2';

In non-strict mode, changing the state will fail silently, as above. In strict mode, changing the state will throw an exception:

model.__.on(state => {
  'use strict';
  try {
    state.name = 'Nopity nope nope';
  } catch (err) {
    console.log(err.message);
  }
});

model.name = '\Pek Example 3';

⇒ Cannot assign to read only property 'name' of object '[object Object]'

API

pek(initialState)

Creates a new Pēk model

  • initialState - Object or Array holding your initial model state
  • Returns a Pēk model (a Proxy-wrapped version of initialState)

E.g.

const model = pek({a: 'hello', b: ['world']}); // ⇨ { a: 'hello', b: [ 'world' ] }

model. ... __.on(callback)

Listen for changes to model state.

  • callback - Function to call any time the target object or any of it's subordinate objects change state. This function takes the following arguments:
    • state - An immutable copy of the object state. Attempting to modify
  • Returns unsubscriber Function.
const unsubscribe = model.__.on(immutableState => console.log(immutableState));

model.a = 'Hello';

⇒ { a: 'Hello', b: [ 'world' ] }

To unsubscribe, call the returned unsubscriber Function:

unsubscribe();

Markdown generated from src/README_js.md by RunMD Logo