{"id":15684108,"url":"https://github.com/xtuc/unmarshaller","last_synced_at":"2025-05-07T15:08:14.123Z","repository":{"id":57386702,"uuid":"87226679","full_name":"xtuc/unmarshaller","owner":"xtuc","description":"Toolbox for configuration","archived":false,"fork":false,"pushed_at":"2018-04-16T20:55:46.000Z","size":52,"stargazers_count":10,"open_issues_count":1,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-09-18T12:52:07.824Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xtuc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-04T19:28:03.000Z","updated_at":"2019-01-15T12:51:54.000Z","dependencies_parsed_at":"2022-09-05T12:21:08.798Z","dependency_job_id":null,"html_url":"https://github.com/xtuc/unmarshaller","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtuc%2Funmarshaller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtuc%2Funmarshaller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtuc%2Funmarshaller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtuc%2Funmarshaller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xtuc","download_url":"https://codeload.github.com/xtuc/unmarshaller/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219870511,"owners_count":16555151,"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":[],"created_at":"2024-10-03T17:11:25.067Z","updated_at":"2024-10-03T17:11:25.670Z","avatar_url":"https://github.com/xtuc.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Unmarshaller\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/xtuc/unmarshaller.svg)](https://greenkeeper.io/)\n\n\u003e Toolbox for configuration\n\n[![Build Status](https://travis-ci.org/xtuc/unmarshaller.svg?branch=master)](https://travis-ci.org/xtuc/unmarshaller)\n\n## Motivations\n\n- A declarative way for defining configuration\n- Reusability and composability\n- Flexible and generic\n\n## Installation\n\n```shell\nnpm install --save unmarshaller\n```\n\n## Example\n\n```js\nimport {builder, unmarshal} from 'unmarshaller';\n\nconst unmarshaller = {\n  editor: builder.string('EDITOR'),\n  browser: builder.string('BROWSER'),\n};\n\nconst lookupFn = (key) =\u003e process.env[key];\nconst config = unmarshal(lookupFn, unmarshaller);\n\nconsole.log(config);\n```\n\n## Basics\n\n### Lookup function\n\nYou need to provide to the `unmarshal` function a way to lookup from keys in configuration.\n\nThe example above returns the value found in the process's environment:\n\n```js\nconst lookupFn = (key) =\u003e process.env[key];\n```\n\n### Unmarshaller\n\nJust an object which represents your configuration.\n\n### Builder\n\nHelper functions to build the unmarshaller object.\n\n#### Default types\n\n| type    |\n|---------|\n| string  |\n| boolean |\n| number  |\n| object  |\n| holder  |\n|    or   |\n\n#### Default options\n\n|name|type|description|\n|----|----|-----------|\n|defaultValue|string|fallback value if the lookup returned `undefined` or `null`|\n|of|array|provide an enumeration of possible values|  fallbacks to `defaultValue` and `null`.|\n|parser|function|provide an custom parser function (usually when you want your own types)|\n\n### Or\n\n```js\nconst unmarshaller = {\n  foo: builder.or(\n    builder.string('foo_a'),\n    builder.string('foo_b'),\n  )\n};\n```\n\nThe unmarshalling process will call the lookup function until a value (not undefined or null) is returned.\n\n### Extending an existing holder\n\n```js\nimport {extend, builder} from 'unmarshaller';\n\nconst holder = builder.holder({\n  editor: builder.string('EDITOR'),\n  browser: builder.string('BROWSER'),\n});\n\nconst extendedHolder = extend(holder, {\n  version: builder.string('VERSION'),\n});\n```\n\n### Extending the default builder\n\nThe builder is a regular JavaScript object.\n\nCustomizing the builder gives you the possiblity to use your own data converters.\n\nThe following example use a custom type: `color` (which in my use case parses a string into a structure).\n\n```js\nimport {builder as defaultBuilder} from 'unmarshaller';\n\nexport const builder = {\n  ...defaultBuilder,\n  color: (name, options) =\u003e ({\n    name,\n    parser: parseColor,\n    type: 'color',\n    ...options\n  }),\n};\n```\n\nYou need to provide a custom parser function (`parseColor` in the example above).\nThe definition is: `function(value: string): string`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtuc%2Funmarshaller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxtuc%2Funmarshaller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtuc%2Funmarshaller/lists"}