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

json-amorph

v0.0.2

Published

transform json

Downloads

1

Readme

Amorph Build Status

Convert JSON to AST, AST to JSON and transformation

Example #1 [example.js]

const {json2ast, ast2json, transform} = require ('./index.js');

const json = {
  hello1: 'hello world',
  hello2: ['hello', 2, true],
  hellonested: {
    nested1: 'nested',
    nested2: ['world', 7, false],
  },
};

const ast = json2ast (json);
const json2 = ast2json (ast);

console.log ('JSON => AST => JSON');
console.log ('ORIGINAL: %O', json);
console.log ('AST: %O', ast);
console.log ('COPY: %O', json2);
console.log ('-------------------');

function pluginKeyReverse (api) {
  return {
    [api.AstType.key]: node => {
      node.value.value = node.value.value.split ('').reverse ().join ('');
    },
  };
}
function pluginNotBoolean (api) {
  return {
    [api.AstType.boolean]: node => {
      node.value = !node.value;
    },
  };
}

const result = transform (json, [pluginKeyReverse, pluginNotBoolean]);
console.log ('TRANSFORM:');
console.log ('ORIGINAL: %O', json);
console.log ('NEW JSON: %O', result.json);
console.log ('NEW AST: %O', result.ast);
console.log ('-------------------');
[gap@localhost amorph]$ npm run demo

> [email protected] demo /home/gap/projects/amorph
> node ./example.js

JSON => AST => JSON
ORIGINAL: { hello1: 'hello world',
  hello2: [ 'hello', 2, true ],
  hellonested: { nested1: 'nested', nested2: [ 'world', 7, false ] } }
AST: { type: 'object',
  value: 
   [ { type: 'pair', value: [Array] },
     { type: 'pair', value: [Array] },
     { type: 'pair', value: [Array] } ] }
COPY: { hello1: 'hello world',
  hello2: [ 'hello', 2, true ],
  hellonested: { nested1: 'nested', nested2: [ 'world', 7, false ] } }
-------------------
TRANSFORM:
ORIGINAL: { hello1: 'hello world',
  hello2: [ 'hello', 2, true ],
  hellonested: { nested1: 'nested', nested2: [ 'world', 7, false ] } }
NEW JSON: { '1olleh': 'hello world',
  '2olleh': [ 'hello', 2, false ],
  detsenolleh: { '1detsen': 'nested', '2detsen': [ 'world', 7, true ] } }
NEW AST: { type: 'object',
  value: 
   [ { type: 'pair', value: [Array] },
     { type: 'pair', value: [Array] },
     { type: 'pair', value: [Array] } ] }
-------------------

Example #2 [simple.sj]

Convert from some general user search request to Elasticsearch Query DSL format

From

{"prompt":"Hello world","offset":5,"limit":15,"language":"en"}

To

{"query":{"query_string":{"query":"Hello world"}},"from":5,"size":15,"type":"en_document"}

Source

const Amorph = require ('./index.js');

const UserSearchRequest = {
  prompt: 'Hello world',
  offset: 5,
  limit: 15,
  language: 'en',
};

function pluginPagination (api) {
  return {
    [api.AstType.key]: key => {
      switch (api.getKeyName (key)) {
        case 'offset':
          key.value = api.createString ('from');
          break;
        case 'limit':
          key.value = api.createString ('size');
          break;
      }
    },
  };
}

function pluginLanguage (api) {
  return {
    [api.AstType.pair]: pair => {
      const kv = api.parsePair (pair);
      if (kv.key === 'language') {
        pair.value = [
          api.createKey ('type'),
          api.createValue (api.createString (kv.value + '_document')),
        ];
      }
    },
  };
}

function pluginPrompt (api) {
  return {
    [api.AstType.pair]: pair => {
      const kv = api.parsePair (pair);
      if (kv.key === 'prompt') {
        pair.value = [
          api.createKey ('query'),
          api.createValue (
            api.createObject ([
              api.createPair (
                'query_string',
                api.createObject ([
                  api.createPair ('query', api.createString (kv.value)),
                ])
              ),
            ])
          ),
        ];
      }
    },
  };
}

const ElasticSearchRequest = Amorph.transform (UserSearchRequest, [
  pluginPagination,
  pluginLanguage,
  pluginPrompt,
]);

console.log ();
console.log (JSON.stringify (UserSearchRequest));
console.log ();
console.log (JSON.stringify (ElasticSearchRequest.json));
console.log ();