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

foxst

v1.0.0

Published

Functional Object eXtraction, Serialization and Transformation for JSON

Downloads

3

Readme

Functional Object eXtraction, Serialization and Transformation (foxst)

Foxst makes it easy to query, extract data from, serialize, and transform any JavaScript object or primitive using a functional programming approach plus regular expressions that can match both object keys and values.

Maintainability Generic badge

Installation

npm install foxst

Usage

Querying, Extracting, and Transforming

import {xt} from "foxst"

Foxst applies a pattern to a target JavaScript object or primitive. The pattern is either a function, RegExp, object or primitive (string,number,boolean). Typically, it will be an object with properties, the values of which are literals, functions, or RegExps.

If a primitive value stored in a pattern property has the same value as property on the target, it is considered to be a match.

xt({name:"joe"},{name:"joe",age:10}); // returns {name: "joe"}

If a RegExp stored in a pattern property matches a value in the same property on a target, it is considered a match.

xt({name:/jo./},{name:"joe",age:10}); // returns {name: "joe"}

If a function stored in a pattern property returns anything other than undefined, then the same property on the target is considered to be a match and the returned value is used as the result.

xt({name:"joe"},{age:10}); // returns undefined because there is not even a name property on the target
xt({name:(value) => value},{name:"joe",age:10}); // returns {name: "joe"}
xt({name:(value) => value,age:(value)=>value},{name:"joe",age:10}); // returns {name: "joe",age:10}
xt({name:(value) => value,age:(value)=>value>10},{name:"joe",age:10}); // returns {}, target does not have age > 10, but does have some matching keys
xt({name:(value) => value==="joe" ? "joe" : undefined},{name:"joe",age:10}); // returns {name: "joe"}
xt({name:(value) => typeof(value)==="string" ? value.toUpperCase() : undefined},{name:"joe",age:10}); // returns {name: "JOE"}
xt({name:(value) => undefined},{name:"joe",age:10}); // returns {}, there is a name property on target, but value is undefined, so it is dropped

If a RegExp stored as a property matches a property on the target and any of the above value matching conditions apply, then there is a match.

xt({[/ag./]:10},{name:"joe",age:10}); // returns {age:10}

If a function stored as a property matches a property on the target and any of the above value matching conditions apply, then there is a match.

xt({[(key) => key==="age" ? key :undefined]:10},{name:"joe",age:10}); // returns {age:10}

A function stored as a property can change the display name of the key.

xt({[(key) => key==="age" ? key.toUpperCase() : undefined]:10},{name:"joe",age:10}); // returns {AGE:10}

Objects are recursively matched.

xt({name:(value) => value,address:{city:(value)=>value,state:"ny"}},{name:"joe",address:{city:"ny",state:"ny"}}); 
// returns {name:"joe",address:{city:"ny",state:"ny"}} 

In order to hide a value used in a match, return null or mask characters, e.g. ****. Or, use a key match and return the special key name anonymous to drop the property from results.

xt({name:(value) => value,address:{city:(value)=>value,state:(value) => value==="ny" ? null : undefined}},{name:"joe",address:{city:"ny",state:"ny"}}); 
// returns {name:"joe",address:{city:"ny",state:null}}
xt({name:(value) => value,address:{city:(value)=>value,[(key) => key=="state" ? "anonymous" : undefined]:"ny"}});
// returns {name:"joe",address:{city:"ny"}}

Serializing and Deserializing

import {serializer,reviver} from "foxst";

function <- serializer(boolean | {functions=boolean,regexs=boolean,infinity=boolean,nan=boolean,builtins=boolean})

Passing false sets all flags to false. The returned function can be used as the second argument to JSON.stringify. The flags instruct the function on whether it should ignore or serialize the items. The builtins flag covers all array types, Set and Map. Custom objects are serialized, although cyclic object can't currently be handled.

function <- reviver(boolean | {functions=boolean,regexs=boolean,infinity=boolean,nan=boolean,builtins=boolean,autocreate=boolean}, {constructors,...})

Passing false sets all flags to false. The returned function can be used as the second argument to JSON.parse. The flags instruct the function on whether it should ignore or restore the items. The builtins flag covers all array types, Set and Map. Custom objects are restored using constructors passed as an object with keys being constructor names values being classes or constructor functions. The second argument is optional. If autocreate is true constructors will be created on the fly. If it is not true and no constructors are provided, custom objects get restored as POJOs.

You can use serializer and reviver to serialize and revive both patterns and targets for transmission over the wire. You should isolate the restoration process to a Worker if you do not have full control over the RegExps and functions used in patterns or malicious code could enter your core application.

Unit Test Results

18 specs, 0 failures Finished in 0.17 seconds ----------------|---------|----------|---------|---------|------------------- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s ----------------|---------|----------|---------|---------|------------------- src | 98.14 | 97.17 | 100 | 98.14 |
index.js | 98.14 | 97.17 | 100 | 98.14 | 217-219,242-243 ----------------|---------|----------|---------|---------|-------------------

Version History (Reverse Chronological Order)

2023-02013 v1.0.0 Improved logic to address multiple matches. Introduced 'anonymous' keys. Added documentation.

2023-02-13 v0.0.2 Code formatting.

2023-02-13 v0.0.1 First public release. 98.7% test coverage. Just lacks documentation.