{"id":18428141,"url":"https://github.com/airtoxin/object-formatter","last_synced_at":"2025-04-13T20:54:21.632Z","repository":{"id":35090380,"uuid":"39249556","full_name":"airtoxin/object-formatter","owner":"airtoxin","description":"format object safely","archived":false,"fork":false,"pushed_at":"2016-11-08T01:13:42.000Z","size":22,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-07T00:44:04.396Z","etag":null,"topics":["fmt","formatter","javascript","schema"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/airtoxin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-17T10:47:07.000Z","updated_at":"2016-11-08T00:27:22.000Z","dependencies_parsed_at":"2022-08-18T05:15:26.550Z","dependency_job_id":null,"html_url":"https://github.com/airtoxin/object-formatter","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/airtoxin%2Fobject-formatter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/airtoxin%2Fobject-formatter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/airtoxin%2Fobject-formatter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/airtoxin%2Fobject-formatter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/airtoxin","download_url":"https://codeload.github.com/airtoxin/object-formatter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248766733,"owners_count":21158301,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fmt","formatter","javascript","schema"],"created_at":"2024-11-06T05:12:54.749Z","updated_at":"2025-04-13T20:54:21.598Z","avatar_url":"https://github.com/airtoxin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# object-formatter [![Build Status](https://travis-ci.org/airtoxin/object-formatter.svg?branch=master)](https://travis-ci.org/airtoxin/object-formatter/branches) [![npm version](https://badge.fury.io/js/object-formatter.svg)](http://badge.fury.io/js/object-formatter)\n\nformat object safely\n\n## Install\n\n`npm i --save object-formatter`\n\n## Usage\n\n```javascript\nimport ObjectFormatter from 'object-formatter';\nconst fmt = new ObjectFormatter();\n\nconst object = {\n    a: 'lorem',\n    b: 'hoge',\n    c: {\n        ca: 'foo',\n        cb: [ 1, 2, 3 ]\n    },\n    d: [\n        { aa: 'a-a', bb: 'b-b' },\n        { aa: 'a--', bb: 'b--' },\n        { aa: '---', cc: 'ccc' }\n    ]\n};\n\nconst schema = {\n    raw: 'raw value',\n    foo: '@a',\n    bar: '@b.c.d=\"ipsum\"',\n    baz: {\n        raw: 111,\n        a: '@c.cb',\n        b: '@c.c.c',\n        c: [ '@d', {\n            hoge: '@aa',\n            fuga: '@bb=\"b default\"'\n        } ],\n        d: [ '@d', '@cc=\"c default\"' ]\n    }\n};\n\nfmt.format(schema, object);\n// -\u003e\n// {\n//     raw: 'raw value',\n//     foo: 'lorem',\n//     bar: 'ipsum',\n//     baz: {\n//         raw: 111,\n//         a: [ 1, 2, 3 ],\n//         b: undefined,\n//         c: [\n//             { hoge: 'a-a', fuga: 'b-b' },\n//             { hoge: 'a--', fuga: 'b--' },\n//             { hoge: '---', fuga: 'b default' }\n//         ],\n//         d: [ 'c default', 'c default', 'ccc' ]\n//     }\n// }\n```\n\nmore examples, see [test file](test/object-formatter.js)\n\n## Documents\n\n`object-formatter` only export ObjectFormatter class as default.\n\n```js\nimport ObjectFormtatter from 'object-formatter';\n```\n\n### Constructor\n\n#### ObjectFormatter(accessorSymbol='@', defaultValue=undefined)\n\n### Instance methods\n\n#### `fmt.format(schema, object)` -\u003e `object`\n\nIt format the object according to the schema definition.\n\n\n### Schema definitions\n\nThe schema must be an object. \nSchema's keys will be the key of the formatted object with no modifications. \nSchema's value means new value. \nIf value is not a string, that value will be the value of the formatted object with no modifications. \nIf value is a string but not start with fmt.accessor(`@`), that value will also be the value of the formatted object with no modifications.\nIf value is string and start with fmt.accessor(`@`), it means new value's path. \nIf path doesn't exists on object, this definition will return fmt.default(`undefined`).\n\n#### Simple accessor (path string)\n\n##### `@path.to.value`\n\nIt accesses `object.path.to.value`. \nFields of object are separate with `.` like a javascript.\n\n```javascript\nconst object = { a: { b: ['this', 'is', 'a.b'] } };\nfmt.format({ result: '@a.b' }, object);\n// -\u003e { result: ['this', 'is', 'a.b'] }\n```\n\n##### `@path.to.value=\"temporary default\"`\n\n`=` means temporary default value. \nThat definition accesses `object.path.to.value` and returns exact value, or `'temporary default'` value when path doesn't exist.\n\n```javascript\nconst object = { a: { b: ['this', 'is', 'a.b'] } };\nfmt.format({ result: '@a.b.c=\"not exists\"' }, object);\n// -\u003e { result: 'not exists' }\n```\n\n#### Collection accessor\n\nThe array value that 1st arg is path string and 2nd arg is schema object or path string, is collection accessor (collection is objected array). \n1st arg path string defines object's collection path. \nIts accessor returns array or collection or default value.\n\n##### `[\"@path.to.collection\", path]`\n\nIt returns array (maybe not collection). \n2nd arg's path refers to collection's element object.\n\n```javascript\nconst object = { a: [{ b: 1 }, { b: 2 }, { b:3 }] };\nfmt.format({ result: ['@a', '@b'] });\n// -\u003e { result: [1, 2, 3] }\n```\n\n##### `[\"@path.to.collection\", schema]`\n\nIt returns collection. \n2nd arg's schema object is schema of collection's element object.\n\n```javascript\nconst object = { a: [{ b: 1 }, { b: 2 }, { b:3 }] };\nfmt.format({ result: ['@a', { new_b: '@b' }] });\n// -\u003e { result: [{ new_b: 1 }, { new_b: 2 }, { new_b: 3 }] }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fairtoxin%2Fobject-formatter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fairtoxin%2Fobject-formatter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fairtoxin%2Fobject-formatter/lists"}