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-scriptify

v0.8.3

Published

Convert a javascript object to a script

Downloads

7

Readme

json-scriptify

The main goal of this module is to produce a readable and minimum length script from an object that when be evaluated, returns an object as same as possible to the original object.

Features

Circular references and repeated references are supported.

Supported built-in objects :

  • Number : primitive type and object
  • Boolean : primitive type and object
  • String : primitive type and object
  • Symbol : primitive type and object
  • BigInt
  • Date
  • RegExp
  • Map
  • ⚠️ WeakMap cannot be converted to script.
  • Set
  • ⚠️ WeakSet cannot be converted to script.
  • Errors : Error, TypeError, EvalError, SyntaxError, RangeError, ReferenceError, URIError
  • ArrayBuffer
  • SharedArrayBuffer
  • Typed Arrays : Int8Array, ... , Uint32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array
  • DataView
  • Function : Only the string code of functions( from toString() ) will be added to the script. We cannot copy variables from closures. Also, methods, getters, setters and generator functions are supported.
    • ⚠️ Bound functions cannot be converted to script.
  • Classes : Only the string code of classes (from toString() ) and their parent classes will be added to the script. We cannot copy variables from closures.

Also, all properties of any object, either enumerables or non enumerables properties, will be converted to script. ( __proto__ is not supported yet)

Some predefined values in javascript such as Symbol.match or window object are defined.

Installation

You can install this module via npm.

npm install json-scriptify

Usage

var scriptify = require("json-scriptify");

let scriptString = scriptify(object, Replacer, options);

Replacer is a function from any object to any object.

options is a object that can contain lineBreak and predefined keys. By default,lineBreak is "\n ".

Simple object with circular

let scriptify = require("json-scriptify");

let obj = { num: 1, str: "string", date: new Date(), re: /any regex/g };
var script = scriptify(obj);

console.log(script);
// ({num:1,str:"string",date:new Date(1601920112100),re:/any regex/g})

obj.circular = obj;
obj.repeated = obj.date;

script = scriptify(obj);

console.log(script);
/*
(function(){
 let obj;
 obj={num:1,str:"string",date:new Date(1601920884331),re:/any regex/g};
 obj.circular=obj;
 obj.repeated=obj.date;
 return obj;
})()
*/

A bit more complex object

let scriptify = require("json-scriptify");

let obj = {
	func: function() {},
	sym: Symbol("1"),
	arr: new ArrayBuffer(4),
	circular: new Set()
};

obj.func.prototype.sym = obj.sym;
obj.circular.add(obj);
obj.circular.add(obj.sym);
obj.circular.add(obj.func);
obj.circular.add(obj.arr);
obj.circular.add(obj.circular);

let int16 = new Int16Array(obj.arr);
int16[0] = 1000;

script = scriptify(obj);

console.log(script);
/*
(function(){
 const _={};
 let obj;
 obj={func:function () {},arr:Uint8Array.from("e8030000".match(/../g),v=>parseInt(v,16)).buffer,circular:new Set()};
 Object.assign(obj.func.prototype,{sym:Symbol("1")});
 obj.sym=obj.func.prototype.sym;
 _.a=[obj,obj.func.prototype.sym,obj.func,obj.arr,obj.circular];
 _.a.forEach(v=>{obj.circular.add(v);});
 return obj;
 })()
*/

Albeit, the size of this script can be less, maybe at later versions.

Replacer

usage of Replacer is like as JSON.stringify Replacer. But without key and this arguments. Only the value that we want to produce script of it, will be passed to Replacer.

let scriptify = require("json-scriptify");

let obj = { num: 1, str: "string", date: new Date(), re: /any regex/g };

let replacer = function(value) {
	if (typeof value == "object" && value !== obj) return;
	return value;
};

var script = scriptify(obj, replacer);

console.log(script);
// ({num:1,str:"string",date:undefined,re:undefined})

If you want to ignore some values from being in the script, you can use scriptify.ignore. If the replacer returns scriptify.ignore, this value will be removed from the script. And, if this value is a key of an object, both the key and its value will be ignored.

If you only want to ignore some properties of an object from the script, you can use scriptify.ignoreSomeProps function. This function takes two arguments. The first is the object and the second is a list of keys that must be ignored.

let obj = {
	num: 1,
	str: "string",
	date: new Date(),
	re: /any regex/g,
	o: { preserve: 1, mustBeIgnored: 2 }
};

let replacer = function(value) {
	if (value instanceof Date) return scriptify.ignore;
	if (typeof value == "object")
		return scriptify.ignoreSomeProps(value, ["mustBeIgnored"]);
	return value;
};

var script = scriptify(obj, replacer);

console.log(script);
// ({num:1,str:"string",re:/any regex/g,o:{preserve:1}})

predefined values

We defined some predefined values in predefinedValues.js file. Until now, we only add global functions such as Map, eval, or String and symbols in the Symbol object such as Symbol.iterator.

You can add your predefined values in options.predefined.

options = {predefined:[ ["script1",value1],["script2",value2], ... ]}

example

let scriptify = require("json-scriptify");

let parentClass = class a {};
let obj = class myExtendedClass extends parentClass {};

var script = scriptify(obj, null, {
	predefined: [["parentClass", parentClass]]
});

console.log(script);
/*
(function(){
 const _={};
 let obj;
 obj=(function(){
 return class myExtendedClass extends parentClass {};
 })();
 _.a={length:{value:0,configurable:true},name:{value:"myExtendedClass",configurable:true}};
 Object.defineProperties(obj,_.a);
 return obj;
 })())
*/

License

MIT