UNPKG

serialize-as-code

Version:

Serialize any Javascript object as its source code

58 lines 4.76 kB
{ "name": "serialize-as-code", "version": "2.0.2", "description": "Serialize any Javascript object as its source code", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", "types": "index.d.ts", "files": [ "dist/cjs", "dist/esm", "index.d.ts" ], "repository": { "type": "git", "url": "https://github.com/fdc-viktor-luft/serialize-as-code" }, "keywords": [ "serialize", "cyclomatic", "stringify", "representation" ], "author": "Viktor Luft <viktor.luft@freiheit.com> (https://github.com/fdc-viktor-luft)", "license": "MIT", "devDependencies": { "@babel/core": "^7.17.8", "@babel/preset-env": "^7.16.11", "@babel/preset-react": "^7.16.7", "@babel/preset-typescript": "^7.16.7", "@rollup/plugin-babel": "^5.3.1", "@rollup/plugin-node-resolve": "^13.1.3", "@types/jest": "^27.4.1", "@types/react": "^17.0.43", "@typescript-eslint/eslint-plugin": "^5.17.0", "@typescript-eslint/parser": "^5.17.0", "babel-jest": "^27.5.1", "coveralls": "^3.1.1", "eslint": "^8.12.0", "eslint-config-prettier": "^8.5.0", "eslint-plugin-prettier": "^4.0.0", "eslint-plugin-react": "^7.29.4", "jest": "^27.5.1", "prettier": "^2.6.1", "react": "^17.0.2", "rollup": "^2.70.1", "typescript": "^4.6.3" }, "scripts": { "dist": "rm -rf dist && rollup -c && tsc -p tsconfig.publish.json && mv dist/index.d.ts index.d.ts && rm dist/index.js", "lint": "pnpm lint:es && pnpm lint:ts", "lint:es": "eslint --ext .ts,.tsx src/ test/", "lint:ts": "tsc", "test": "pnpm lint && pnpm test:coverage", "test:coverage": "jest --all --coverage", "coveralls": "cat ./coverage/lcov.info | coveralls" }, "readme": "[![GitHub license][license-image]][license-url]\n[![npm package][npm-image]][npm-url] \n[![Travis][build-image]][build-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n\n# serialize-as-code\n\nThis serializer is intended for serializing ANY Javascript objects into\nits source code representation. It is cyclomatic-save extension and alternative\nto e.g. `JSON.stringify`. It can even be used to perform deep comparisions\nfor most cases. ATTENTION: There is no functionality to parse the string back\nto the provided object. (one-direction-flow)\n\n### Some aspects\n- `TypeScript` support included\n- ~ 1 kB (gzipped) (see [bundlephobia](https://bundlephobia.com/result?p=serialize-as-code))\n\n### Installation\n##### With yarn\n```\nyarn add serialize-as-code # if you want to use it for prod\nyarn add --dev serialize-as-code # if only used in tests\n```\n\n### Usage\nYou may import the named import `Serializer` and call run with ANY object.\nIt prints the object as its source code. So you should be able to copy-paste\nthe printed result and most deep comparisons should work.\n```js\nimport { Serializer } from 'serialize-as-code';\n\n// Arrays or Objects\nconsole.log(Serializer.run({foo: 'bar'})); // prints: {foo: 'bar'}\nconsole.log(Serializer.run([1, 2])); // prints: [1, 2]\n\n// Symbols (not suppoerted on JSON.stringify)\nconsole.log(Serializer.run(Symbol.for('my-key')));\n// prints: Symbol.for('my-key')\n\n// JSX (but react is not needed and their is no dependency)\nconsole.log(Serializer.run(<Fragment key=\"foo\"><div>Test that</div></Fragment>));\n// prints: <Fragment key=\"foo\"><div>Test that</div></Fragment>\n```\nIf you try to serialize cyclomatic structures, you will see within the result\nwere they were detected.\n\n### Custom serialization\nYou have the possibility to apply ANY custom serialization just on top.\n```js\nimport { Serializer } from 'serialize-as-code';\n\nconst someObjectToMatch = { foo: 'foo' };\n\nconst CustomSerializer = (o: any): string | void => {\n if (o === someObjectToMatch) return 'FOO';\n // return undefined to proceed with regular serialization\n};\n\nconst YourSerializer = Serializer.create(CustomSerializer);\n\nconsole.log({ canBe: 'nested', here: someObjectToMatch });\n// prints: {canBe: 'nested', here: FOO}\n```\n\n \n[license-image]: https://img.shields.io/badge/license-MIT-blue.svg\n[license-url]: https://github.com/fdc-viktor-luft/form4react/blob/master/LICENSE\n[build-image]: https://img.shields.io/travis/fdc-viktor-luft/serialize-as-code/master.svg?style=flat-square\n[build-url]: https://app.travis-ci.com/github/fdc-viktor-luft/serialize-as-code\n[npm-image]: https://img.shields.io/npm/v/serialize-as-code.svg?style=flat-square\n[npm-url]: https://www.npmjs.org/package/serialize-as-code\n[coveralls-image]: https://coveralls.io/repos/github/fdc-viktor-luft/serialize-as-code/badge.svg?branch=master\n[coveralls-url]: https://coveralls.io/github/fdc-viktor-luft/serialize-as-code?branch=master\n" }