declarative immutable-ish data structures
Structy aims to provide a flexible and declarative api for managing your application's data. There are other great libraries out there such as Immutable, but Structy provides a bit more sugar to make managing your data easier.
getting started
install with npm
$ yarn add structy
import into your project
;// const { Model, Collection } = require('structy');
or add via global (use structy < 2.0.0)
<script src="https://cdn.jsdelivr.net/npm/structy@1.0.1/dist/structy.js" type="text/javascript"/>
<script type="text/javascript>
const { Model, Collection } = structy;
...
</script>
Usage
below is an example of how you might use Model
and Collection
to structure your burger app's data.
we would start by defining the schema for our ingredients
// extend model and specify the getters and setters for your specific Model// base ingredient e.g. pickles { superdata; } { return thistype; } { return thiscost; } // Use Collections for structuring collections of data// collection of ingredients with helpers for getting at item data { superitems Ingredient; } // collections are a good place for common methods for interacting with the data { return this; }
Then we would define the schema for our burger, which includes ingredients
// a burger and its cost are made up of its ingredientsconst burgerConfig = cost: default: 3 // base cost is 3 ingredients: IngredientCollection // Burger will wrap all 'ingredient' fields with a IngredientCollection; { // call Model constructor and pass the burger data and fieldConfig superdata burgerConfig; } // even though the consumer of this Burger Model could use Burger.name, it is better // to document the shape of your data via getters and setters. { return thisname; } { return thisfavorite; } { // note: setters are required since the data is immutable // this.favorite = fav will throw return this; } { // here we reuse the IngredientCollection cost logic return thiscost + thisingredients; } { return thisingredients; } { return thisingredients; } { superitems Burger; } { // again building on what we have already done, we reuse the IngredientCollection and Burger cost logic return this; } { return this; } { return this; }
finally we would define/fetch the data and start using it
const hamburger = name: 'Hamburger' ingredients: type: 'beef' cost: 1 type: 'ketchup' cost: 05 type: 'mustard' cost: 05 type: 'lettuce' cost: 025 type: 'tomato' cost: 1 type: 'pickle' cost: 025 ; const cheeseBurger = name: 'Cheese Burger' ingredients: ...hamburgeringredients type: 'cheese' cost: 1 ; const order = hamburger;console; // order items: ['Hamburger']console; // order cost: 6.5 const updatedOrder = order;console; // order items: ['Hamburger', 'Cheese Burger']console; // order cost: 14console;
API
Model
- data {object} - data that the model will operate on.
- fieldConfig {object} optional - specifies options for each field of the model.
- filter {function} optional - filter data, should return filtered data
example field config:
const fieldConfig = '*': Number // specify wildcard to pass the value of each field in data to Number constructor 'foo': Model // wildcard only applies to fields not defined 'bar': nullable: false //fields with undefined/null values are ignored by default => data.baz.one ? null : true }
Collection
- items {array} - initial items in the collection.
- ItemConstructor {constructor} - constructor to pass item(s) to.
More API documentation coming shortly, until that time checkout Model and Collection