0.3.0 • Published 2 years ago

@zerodep/to.json v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@zerodep/to.json

minified size minified+gzipped size tree shaking language types

coverage last commit vulnerabilities

app version

A configurable function that converts a value to a JSON object.

This includes converting Date, Map, Set, and BigInt values. If an object or class with a toJSON() method is found, the result of calling the method will be used.

The toJSON method can be optionally configured to convert non-JSON-able values (e.g. Symbol or WeakMap) to null instead of throwing errors.

The function returns a plain JSON object, which means an object literal, an array or null. This object could be serialized (converted to a string) without affecting data integrity.

tl;dr

A short explanation / quick reference:

import { toJSON } from '@zerodep/to.json';

// uses the default configuration options
toJSON({ one: 1, two: 2 }); // { "one": 1, "two: 2 }
toJSON(['a', 'b', 'c']); // ["a", "b", "c"]
toJSON(42); // throws ZeroDepErrorTo
toJSON({ wm: new Promise(() => {}) }); // throws ZeroDepErrorTo

and

import { toJSONHOF, ToJSONOptions } from '@zerodep/to.json';

// uses a custom configuration options
const options: ToJSONOptions = { convertInvalidToNull: true };
const toJSON = toJSONHOF(options);

toJSON({ one: 1, two: 2 }); // { "one": 1, "two: 2 }
toJSON(['a', 'b', 'c']); // ["a", "b", "c"]
toJSON(42); // still throws ZeroDepErrorTo
toJSON({ wm: new Promise(() => {}) }); // { "wm": null }

Apologies for the mess of capital letters, however both JSON and HOF are abbreviations. Not keeping either one uppercase would violate the naming patterns of the @zerodep packages.

Table of Contents

Install

This utility is available from multiple @zerodep packages, enabling developers to select the most appropriately sized package (for both kb and capability) for different use cases. We believe one size does not fit all or most. See @zerodep/app, @zerodep/utils and @zerodep/is.

For Server & Build Tooling

For Node, or when compiling via babel, rollup, swc, tsc, webpack, etc... these are the instructions for you.

// all @zerodep features, capabilities and utilities
npm install @zerodep/app

// entire set of @zerodep utilities
npm install @zerodep/utils

// all @zerodep "is" utilities
npm install @zerodep/is

// only the to.json utility
npm install @zerodep/to.json

Of course, you may use yarn, pnpm, or the package manager of your choice. Only npm examples are shown for brevity.

Browser Direct

If you are using the script directly in a browser via a <script> tag or importing it into your own scripts, these are the instructions for you. We support both ESM and UMD formats.

<!-- for ES Modules (ESM) -->
<script type="module">
  import { toJSON } from 'https://cdn.jsdelivr.net/npm/@zerodep/to.json/esm.js';
  // ...your code here
</script>

<!--  OR  -->

<!--  for Universal Modules (UMD) - all @zerodep functions are in the global "zd" namespace -->
<script src="https://cdn.jsdelivr.net/npm/@zerodep/to.json/umd.js"></script>
<script>
  // example of "zd" prefix
  const result = zd.toJSON({ a: 'one', b: 'two' });
</script>

This package may be found on both jsDelivr and unpkg in UMD, ESM and CJS formats.

How to Use

This package exports the following:

  • Functions
    • toJSON - a converter that uses the default configuration options (suitable for most)
    • toJSONHOF - a higher-order function that may be configured and returns a converter function based on the configurations
  • Interface
    • ToJSONOptions - a typescript interface of the options that may be set in the HOF
  • Error types
    • ZeroDepErrorTo - the subclass of error thrown by all @zerodep/to.* packages
    • ZeroDepError - the error class all ZeroDep packages extend from, is an instance of the base Error object

Signature

Typescript declarations:

// using default configuration options
declare const toJSON: (value: any | any[]) => any[] | Record<string, any> | null;

// customizing the configuration options
declare const toJSONHOF: <T = any[] | Record<string, any>>(
  options?: ToJSONOptions
) => (value: any | any[]) => T | null;

// optional configuration
interface ToJSONOptions {
  convertInvalidToNull?: boolean; // default is false
}

Configuration Options

convertInvalidToNull:

  • Defaults to: false
  • If true it will convert any non-JSON-able properties to null instead of throwing the appropriate ZeroDepError

Examples

All examples assume ESM or CJS packages. If using a UMD package remember to prefix with the zd namespace, e.g. zd.toJSON(...).

Using Default Configuration Options

// import from the most appropriate @zerodep package for your needs / specific use case (see the Install section above)
import { toJSON } from '@zerodep/to.json';

// use, returns an object literal, an array or null (or throws an error)
toJSON({}); // {}
toJSON({ a: 'one', b: 'two' }); // { "a": 'one', "b": 'two' }

toJSON([]); // []
toJSON([1, 2, 3]); // [1, 2, 3]
toJSON(['a', 'b', 'c']); // ["a", "b", "c"]

toJSON(new Set()); // []
toJSON(new Set([1, 2, 3])); // [1, 2, 3]
toJSON(new Map()); // {}
toJSON(new Map([['a', 1]])); // [["a", 1]]

toJSON(null); // null
toJSON(undefined); // null

toJSON({
  string: 'a string',
  date: new Date('2022-02-24'),
  int: 42,
  float: 3.14,
  bigInt: 8675309n,
  boolT: true,
  boolF: false,
});
// {
//   "string": "a string",
//   "date": "2022-02-24T00:00:00.000Z",
//   "int": 42,
//   "float": 3.14,
//   "bigInt": "8675309", <-- CAUTION: BigInt are converted to strings
//   "boolT": true,
//   "boolF": false,
// }

// object properties that cannot be toJSONed to JSON
toJSON({ key: new WeakMap() }); // throws ZeroDepErrorTo
toJSON({ key: new WeakSet() }); // throws ZeroDepErrorTo
toJSON({ key: new Promise(() => {}) }); // throws ZeroDepErrorTo
toJSON({ key: new Error(() => {}) }); // throws ZeroDepErrorTo
toJSON({ key: Symbol() }); // throws ZeroDepErrorTo
toJSON({ key: () => {} }); // throws ZeroDepErrorTo

// strings
toJSON(''); // throws ZeroDepErrorTo
toJSON('a string'); // throws ZeroDepErrorTo

// integers
toJSON(42); // throws ZeroDepErrorTo
toJSON(3e8); // throws ZeroDepErrorTo

// floats
toJSON(-273.15); // throws ZeroDepErrorTo
toJSON(Math.PI); // throws ZeroDepErrorTo

// number-ish
toJSON(Number.POSITIVE_INFINITY); // throws ZeroDepErrorTo
toJSON(NaN); // throws ZeroDepErrorTo

// bigints
toJSON(8675309n); // throws ZeroDepErrorTo

// booleans
toJSON(true); // throws ZeroDepErrorTo
toJSON(false); // throws ZeroDepErrorTo

// other
toJSON(/^$\d{7}/g); // throws ZeroDepErrorTo
toJSON(new Date()); // throws ZeroDepErrorTo
toJSON(new Date('2022-02-24')); // throws ZeroDepErrorTo
toJSON(Symbol()); // throws ZeroDepErrorTo
toJSON(new Error()); // throws ZeroDepErrorTo
toJSON(() => {}); // throws ZeroDepErrorTo

Using Customized Configuration Options

// import from the most appropriate @zerodep package for your needs / specific use case (see the Install section above)
import { ToJSONOptions, toJSONHOF } from '@zerodep/to.json';

const options: ToJSONOptions = { convertInvalidToNull: true };
const toJSON = toJSONHOF(options);

// all examples the same as the simple example above, EXCEPT the following now return null instead of an error
toJSON({ key: new WeakMap() }); // { "key": null }
toJSON({ key: new WeakSet() }); // { "key": null }
toJSON({ key: new Promise(() => {}) }); // { "key": null }
toJSON({ key: new Error(() => {}) }); // { "key": null }
toJSON({ key: Symbol() }); // { "key": null }
toJSON({ key: () => {} }); // { "key": null }

ZeroDepErrorTo Example

// import from the most appropriate @zerodep package for your needs / specific use case (see the Install section above)
import { toJSON } from '@zerodep/to.json';

try {
  toJSON('invalid JSON');
} catch (error: any) {
  console.log(error.message); // "Cannot convert to JSON"
  console.log(error.category); // "syntax"
  console.log(error.source); // "to"
  console.log(error.value); // "invalid JSON" <-- value that caused the error

  // inheritance chain
  error instanceof ZeroDepErrorTo; // true
  error instanceof ZeroDepError; // true
  error instanceof Error; // true
}

Configuration via Higher Order Function

Let's begin with a definition to ensure a common vocabulary: a Higher Order Function (HOF) is just a function that returns another function.

This package uses a Higher Order Function as a way to set up/configure its functionality for:

  • cleaner code: having to pass configuration options once instead of to every call to the function making your code easier to read and reason about
  • improved performance: any time a set of configuration options is passed to a function, it is merged with some default values, doing this once means fewer CPU cycles and memory consumption
  • future scalability: if/when additional configuration options are available they will have no impact on your existing code and will be easier to add should you wish to use them
  • consistency: all @zerodep packages that may be configured follow the same pattern, making the Developer Experience (DX) just a little sweeter

Related Packages

The following @zerodep packages may be helpful or more appropriate for your specific case:

Advantages of @zerodep Packages

We help make source code more readable, more secure, faster to craft, less likely to have hidden defects, and easier to maintain.

  • Zero npm dependencies - completely eliminates all risk of supply-chain attacks, decreases node_modules folder size
  • Fully typed - typescript definitions are provided for every package for a better developer experience
  • Semantically named - package and method names are easy to grok, remember, use, and read
  • Documented - actually useful documentation with examples and helpful tips
  • Intelligently Packaged - multiple npm packages of different sizes available allowing a menu or a-la-carte composition of capabilities
  • 100% Tested - all methods and packages are fully unit tested
  • ESM & CJS - has both ecmascript modules and common javascript exports, both are fully tree-shakable
  • CDN Available - available on fast content delivery networks in UMD, CJS and ESM formats
  • FP Inspired - gently opinionated to encourage functional programming style for cleaner and more maintainable software
  • Predictably Versioned - semantically versioned for peace-of-mind upgrading, this includes changelogs
  • MIT Licensed - permissively licensed for maximum usability

Support

All @zerodep packages are built for the ES2020 specification. Should you need to support older environments you will need to add appropriate polyfills. All packages are tested on the following platforms/browsers:

Browsers

  • Chrome - last 2 major versions
  • Firefox - last 2 major versions
  • Safari - last 2 major versions
  • Edge - last 2 major versions
  • Android - last 2 major versions
  • iOS - last 2 major versions

Node

  • v16.x - Gallium LTS
  • v14.x - Fermium LTS

It is likely the package will work on other technologies and version, however development and testing effort is only spent on the above.

Semver

All @zerodep packages, including this one, adhere to Semantic Versioning practices:

  • major versions: correlates with breaking changes to one or more method signatures
  • minor versions: includes addition of new functionality or backwards-compatible software improvements
  • patch versions: are reserved for copy changes, documentation enhancements and bug fixes

The above said, a security best practice is to pin your software packages to specific versions and only upgrade to more recent releases after careful inspection of any CHANGELOG, release notes and all software changes.

Resources

License

MIT

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.4

2 years ago