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

semantik

v1.1.0

Published

Iterate over arrays and objects semantically, assigning values, validating and parsing

Downloads

3

Readme

#SemantiK

This is a library to iterate over arrays and objects semantically, assigning values, validating and parsing. From an object, JSON or array we can create another object using a declarative and semantic function.

##How does it work:

From this JSON:

var source = {
  products: [{
    name: 'Node JS Design Patterns',
    units: '5'
  },
  {
    name: 'T-Shirts NodeJS',
    units: '10'
  }]
};

####semantik.compose(source, semantik); Returns an object whose keys are a string with the full path of the properties of the object source. The values are the same as the values of the object semantik.

semantik.compose(source, { 'products[].name': 'some name', 'products[].units': 'some units' });
// {
//   'products[0].name': 'some name',
//   'products[1].name': 'some name'
//   'products[0].units': 'some units',
//   'products[1].units': 'some units'
// }

####semantik.validate(source, semantik); Returns true if all functions of the object semantik returns true, when the functions receive as parameter the values of the object source. Otherwise it returns false. The functions of object semantik must return a boolean value.

semantik.validate(source, { 'products[].name': 'isString' });
// true

You can use the pseudo-method isRequired to ensure that a property exists on the object source.

semantik.validate(source, { 'products[].asdf': 'isRequired' });
// false

####semantik.parse(source, semantik); Returns a copy of the object source, modified by the functions of the object semantik. The functions of object semantik must return a value.

semantik.parse(source, { 'products[].units': parseInt });
// {
//   products: [{
//     name: 'Node JS Design Patterns',
//     units: 5
//   },
//   {
//     name: 'T-Shirts NodeJS',
//     units: 10
//   }]
// }

####Functions of object semantik:

  • The values of object semantik can be a function or an array of functions:
    • { 'products[].units': 'isString' }
    • { 'products[].units': ['isString', 'isNotEmpty'] }
  • The functions of object semantic can be a real function or the name of a function of this library:
    • { 'products[].units': _.isString }
    • { 'products[].units': 'isString' }
  • To pass parameters to the function you can use an object:
    • { 'products[].units': [ { cb: isNumberBetween, params: [5, 25] ] } } // isNumberBetween will receive the parameters: 'value', 5 and 25
    • { 'products[].units': [ { cb: isNumberBetween, params: { min: 5, max: 25 } } ] } // isNumberBetween will receive the parameters: 'value' and { min: 5, max: 25 }
    • { 'products[].units': [ { cb: isNumberGreatThan, params: 5 } ] } // isNumberGreatThan will receive the parameters: 'value' and 5
  • To pass a context to the function you can use an object:
    • { 'products[].units': [ { cb: sort, ctx: this } ]
  • All functions of object semantik receive as first parameter the value of object source.

####Parse helpers:

semantik.parseString(value)

####Validate helpers:

semantik.isEmail(value)

semantik.isNotEmpty(value)

semantik.isNumberGreatThan(value, min, equal = true)

semantik.isNumberLessThan(value, max, equal = true)

semantik.isNumberBetween(value, min, max, equal = true)

semantik.isStringNotEmpty(value)

semantik.isStringNumber(value)

####Lodash methods:

semantik.parseCamelCase(value)

semantik.sortBy(value, predicate)

semantik.isArray(value)

semantik.isEmpty(value)

semantik.isNumber(value)

semantik.isString(value)

####semantik.mixin({ name: method }); Add new functions to this library, so that they can be used in any time.

semantik.mixin({ isString: _.isString });
semantik.validate(source, { 'products[].units': 'isString' });

##When is it useful: For example this library is useful to validate and parse the JSON that are received and sent from a server, in order to ensure that the input and output of an application are correct.

##Examples: Look at the folder examples/ to see how easy it is to use this library.

##Tests: Coming soon...