UNPKG

json-order

Version:

Control the order of properties in JSON via a lookup object - including nested properties.

22 lines (21 loc) 1.28 kB
"use strict"; /* eslint-disable @typescript-eslint/ban-types */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var order_1 = __importDefault(require("./order")); /** * Stringify a JS object while maintaining a particular property order: * * @param sourceObject an object with the properties in any order * @param map the `PropertyMap` generated by the `parse` method. * @param separator a non-empty `string` that controls what the key separator is in the generated map. Defaults to `~`. * @param space a `number` used to insert white space into the output JSON string for readability purposes, as per the `JSON.stringify` [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters) * @returns the stringified source object ordered as per the map. If the map is unset, the response is a standard `JSON.stringify`. */ var stringify = function (sourceObject, map, separator, space) { if (separator === void 0) { separator = '~'; } return JSON.stringify(order_1.default(sourceObject, map, separator), null, space); }; exports.default = stringify;