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

@programmerraj/jsonf

v1.0.0

Published

Json with functions.

Downloads

3

Readme

Test License TS-Standard - Typescript Standard Style Guide

jsonf

Json with functions.

Why

Json is good because of it's simplicity, but it can't do logical things. By being able to call functions, you can do many things with json.

Let's say we want to create an array of 100 numbers from 0 to 99.

[
  0,
  1,
  2,
  "That's a lot of numbers"
]

We could store the data in this long array, but there are a few problems.

  1. It takes up a lot of space. This can make it hard to read.
  2. It's hard to change. If you change your mind and only want to have 50 numbers, or want to add 100 more numbers, then you'll have to do a lot of deleting / adding.

The solution: jsonf.

{
  "$jsonf": ["jsFn", 100]
}

Your javascript function could look like this:

const jsFn = numberOfNumbers => {
  const arr = []
  for (let i = 0; i < numberOfNumbers; i++) {
    arr.push(i)
  }
  return arr
}

Now the json is very easy to read, and you can easily change the number of numbers by changing the 100 to something else, like 50 or 200.

Syntax

'Calling' a function.

You can 'call' a function by having an object with a $jsonf key. The value is an array, where the first element is the name of the function, and the following elements are the parameters given to that function.

{
  "$jsonf": [
    "nameOfMyFn", 
    "param 1", 
    { 
      "object": "is param 2",
      "params": "Can be any json value"
    }
  ]
}

This will transform into whatever nameOfMyFn returned.

"Whatever `nameOfMyFn` returned"

Security

Can random js code be executed?

No. Inside the json, functions can only be called. In order for a function to be callable, they must be passed to the jsonf function.

Install

@programmerraj/transform-json is a peerDependency.

npm i @programmerraj/transform-json @programmerraj/jsonf

Usage

More information about transform-json

ES

import jsonf from 'jsonf'

CommonJS

const jsonf = require('jsonf').default

Transforming Json

jsonf accepts an object where the keys are the names of the functions and the values are the functions.

transform({
  $jsonf: ['myFn', 5, 4]
}, [jsonf({
  myFn: (n1, n2) => n1 * n2
})])`

Transforms into 20.

Using with jsonv

Make sure to put the jsonv visitor before the jsonf visitor.

transform({
  $jsonv: {
    n: 50
  },
  key: {
    $jsonf: ['add', { $jsonv: 'n' }, 2]
  }
}, [
  jsonv(),
  jsonf({ add: (a, b) => a + b })
])

Transforms to

{
  "key": 52
}

In the example above, the variables are getting converted first, which means that add(50, 2) is being called. If we didn't have the jsonv before the jsonf visitor, then add(50, { $jsonv: 'n' }) would be called and add would try to add a number and an object.

Contributing

JavaScript Style Guide