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

jslt

v0.2.13

Published

JSON transformer

Downloads

252

Readme

jslt

var jslt = require("jslt");
jslt.transform({ 
    firstName: "Chandler",
    lastName: "Bing",
    married: true,
  }, {
    fullName: "{{lastName}}, {{firstName}}",
    status: {
      $fetch: "{{married}}",
      $translate: [
        { from: true, to: "Married" },
        { default: "Single" }
      ]
    }
  }
);

Table of contents

Transformation basics

When transforming an object, the properties in the template are recursivly traversed and their output values are determined using the following rules:

  1. If the template value is a string containing placeholders, they are replaced according to the placeholder replacement rules.
  2. If the template value is an object that at least one of its properties starts with a dollar sign, the object is processed using the update rules.
  3. Otherwise the template value is copied to the output.

Placeholder replacement

Placeholders are defined using double curly braces ({{name}}). The text inside is interpreted as the name of a property in the object being transformed. The result of the placeholder is the value of the same named property in the object being transformed. Nested properties can be accessed using a dot notation (e.g. {{prop1.prop2}}).

If a string contains a single placeholder without any other character, the returned value will be of the same type as the original value. Otherwise, it will be converted into a string.

Update rules

Operators are processed in the order they are defined in the object. The output of the first operator becomes the input for the second operator and so forth. If an operator needs to be repeated more than once in the same object, a number can be added to its name (e.g. $translate2).

Update operators

$join

  • Input None
  • Parameters <Array> The array to join
  • Output <String>

Example:

var res = jslt.transform({}, { $join: [ "Hello", " ", "world" ] });
console.log(res); // => "Hello world"

$concat

Merges two or more arrays.

  • Input None
  • Parameters <Array> An array of arrays to concat
  • Output <Array>

Example:

var res = jslt.transform({}, { $concat: [ [ 1, 2 ], [ 3, 4 ] ] });
console.log(res); // => [ 1, 2, 3, 4 ]

$formatDate

Returns a string with a language sensitive representation of the input date. See Date.prototype.toLocaleString() documentation for more information.

  • Input <Date> | <Number> | <String> If not a date object, value should be in a format supported by the Date constructor
  • Parameters
    • locales <String> Optional
    • options <Object> Optional
  • Output <String>

$formatNumber

Returns a string with a language sensitive representation of the input number. See Number.prototype.toLocaleString() documentation for more information.

  • Input <Number> The number to format
  • Parameters
    • locales <String> Optional
    • options <Object> Optional
  • Output <String>

Example:

var res = jslt.transform(1000, { $formatNumber: { locales: "en-US" options: { minimumFractionDigits: 2 } } });
console.log(res); // => "1,000.00"

$parseNumber

Parses the input string and returns a number.

  • Input <String> The string to parse
  • Parameters
    • groupSymbol <String> Optional
    • decimalSymbol <String> Optional
  • Output <Number>

Example:

var res = jslt.transform("1,000", { $parseNumber: { groupSymbol: "," } });
console.log(res); // => 1000

$parseDate

Parses the input string and returns a date object.

  • Input <String>
  • Parameters
    • format <String>
    • timezone <String>
  • Output <Date>

$replace

Returns a new string with some or all matches of a pattern replaced by a new string.

  • Input <String>
  • Parameters
    • substr <String> A string that is to be replaced by newSubStr
    • regexp <RegExp>
    • newSubstr <String> The string that replaces the substring specified by the regexp or substr parameters
  • Output <String>

Example:

var res = jslt.transform("Hello world", { $replace: { substr: "world", newSubstr: "world!!" });
console.log(res); // => "Hello world!!"

$fetch

Transforms the input data using the supplied sub-template and returns the result. Typically used as a starting point for other operators

  • Input None
  • Parameters <Any> The sub-template
  • Output <Any>

Example:

var res = jslt.transform({ someField: "test" }, { $fetch: "{{someField}}" });
console.log(res); // => "test"

With another operator:

var res = jslt.transform({ arrayField: [ 2, 4, 6 ] }, {
  $fetch: "{{arrayField}}",
  $sum : {}
});
console.log(res); // => 12

$translate

Selects a value by applying from-to rules on the input value and returns it. If none of the rules match, returns the input value.

Rules can contain query operators for complex translation logic.

  • Input <Any>
  • Parameters <Array> And array of from-to rules (see examples)
  • Output <Any>

Example:

var res = jslt.transform(true, {
  $translate: [
    { from: true, to: "Yes" },
    { from: false, to: "No" }
  }
);
console.log(res); // => "Yes"

With query operators:

var res = jslt.transform(10, {
  $translate: [
    { from: { $lt: 8 }, to: "Low" },
    { from: { $gte: 8 }, to: "High" }
  ]
});
console.log(res); // => "High"

The above example with a default value:

var res = jslt.transform(10, {
  $translate: [
    { from: { $lt : 8 }, to: "Low" },
    { default: "High" }
  ]
});
console.log(res); // => "High"

$map

  • Input <Array>
  • Parameters <Any>
  • Output <Array>

$filter

  • Input <Array>
  • Parameters <Any>
  • Output <Array>

$push

Adds an element to the end of an array.

  • Input <Array>
  • Parameters <Any> The element to add
  • Output <Array>

Example:

var res = jslt.transform([ 1, 2 ], { $push: 3 });
console.log(res); // => [ 1, 2, 3 ]

$unshift

Adds an element to the beginning of an array.

  • Input <Array>
  • Parameters <Any> The element to add
  • Output <Array>

Example:

var res = jslt.transform([ 1, 2 ], { $unshift: 0 });
console.log(res); // => [ 0, 1, 2 ]

$reverse

Reverses an array.

  • Input <Array>
  • Parameters None
  • Output <Array>

Example:

var res = jslt.transform([ 1, 2, 3 ], { $reverse: {} });
console.log(res); // => [ 3, 2, 1 ]

$sum

Returns the sum of all the elements in an array.

  • Input <Array>
  • Parameters None
  • Output <Number>
var res = jslt.transform([ 1, 2, 3 ], { $sum: {} });
console.log(res); // => 6

$assign

Copies the properties from the parameters to the input object

  • Input <Object>
  • Parameters <Object>
  • Output <Object>
var res = jslt.transform({ prop1 : "value1" }, { $assign: { prop2 : "value2" } });
console.log(res); // => { prop1 : "value1", prop2 : "value2" }

$blacklist

Removes the specified properties from the input object

  • Input <Object>
  • Parameters <Array>
  • Output <Object>
var res = jslt.transform({ prop1 : "value1", prop2 : "value2" }, { $blacklist: [ "prop2" ] });
console.log(res); // => { prop1 : "value1" }

$whitelist

Removes all non specified properties from the input object

  • Input <Object>
  • Parameters <Array>
  • Output <Object>
var res = jslt.transform({ prop1 : "value1", prop2 : "value2" }, { $whitelist: [ "prop2" ] });
console.log(res); // => { prop2 : "value2" }

Query operators

$eq

$nq

$lt

$gt

$lte

$gte

$in

$nin

$regex

$type

Assert keywords

enum

multipleOf

maximum

exclusiveMaximum

minimum

exclusiveMinimum

maxLength

minLength

maxItems

minItems

maxProperties

minProperties

required