{"id":15434013,"url":"https://github.com/sebinsua/jstruct","last_synced_at":"2025-04-19T18:08:37.786Z","repository":{"id":57286477,"uuid":"14192911","full_name":"sebinsua/jstruct","owner":"sebinsua","description":":bulb: Quick and easy JSON transformations.","archived":false,"fork":false,"pushed_at":"2016-05-19T23:48:29.000Z","size":30,"stargazers_count":49,"open_issues_count":6,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-09T02:03:48.462Z","etag":null,"topics":["json","map","notation","transformation"],"latest_commit_sha":null,"homepage":"","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/sebinsua.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":"2013-11-07T03:12:51.000Z","updated_at":"2023-12-31T22:16:28.000Z","dependencies_parsed_at":"2022-09-20T00:20:20.989Z","dependency_job_id":null,"html_url":"https://github.com/sebinsua/jstruct","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebinsua%2Fjstruct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebinsua%2Fjstruct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebinsua%2Fjstruct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebinsua%2Fjstruct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebinsua","download_url":"https://codeload.github.com/sebinsua/jstruct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249758494,"owners_count":21321535,"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":["json","map","notation","transformation"],"created_at":"2024-10-01T18:36:43.189Z","updated_at":"2025-04-19T18:08:37.754Z","avatar_url":"https://github.com/sebinsua.png","language":"JavaScript","readme":"# jstruct [![Build Status](https://travis-ci.org/sebinsua/jstruct.png)](https://travis-ci.org/sebinsua/jstruct) [![npm version](https://badge.fury.io/js/jstruct.svg)](https://npmjs.org/package/jstruct)\n\u003e :bulb: Quick and easy JSON transformations.\n\nJstruct allows quick and easy JSON transformations through the use of a declarative JSON DSL.\n\n```javascript\nimport j, { sel, escape } from 'jstruct';\nimport { curry } from 'ramda';\n\n// ... getAccount() definition\n\nconst prefix = curry((pre, str) =\u003e pre + str);\nconst format = j({\n  id: 'account/id',\n  name: 'account/name',\n  type: escape('human'),\n  hasAddress: sel.isNotEmpty('account/address'),\n  lastPaymentAmount: sel('account/paymentHistory[0]/amount', prefix('£'))\n});\n\ngetAccount().then(format);\n// -\u003e\n// {\n//   id: 5,\n//   name: 'Seb',\n//   type: 'human',\n//   hasAddress: true,\n//   lastPaymentAmount: '£50'\n// }\n```\n\n## Why?\n\nConverting between different data representations in JavaScript is easier than many languages because of the concise and declarative JSON. However, currently [both imperative and functional approaches create unnecessarily verbose code](https://github.com/sebinsua/jstruct/wiki/Premise).\n\nJstruct [employs the visuospatial meaning of JSON as its means of describing the structure of the data we wish to compute](https://twitter.com/sebinsua/status/598828999423438848).\n\n## Usage\n\n#### `j(definition, object)`\n\nThis accepts two arguments: `definition`, the definition of the object that should be returned from the transformation, and `object` the data that should be transformed.\n\nA definition is a collection of selector strings/instances - the former being converted into the latter on execution. It can be a (deep) object/array or even just a single selector. \n\nThis function is [curried by default](http://en.wikipedia.org/wiki/Currying). If you do not supply all of the arguments required for computation it will return a function with the arguments already supplied bound to it.\n\nThis makes it very simple to create formatting functions that expect objects and return transformations of the objects passed in.\n\ne.g.\n\n```javascript\n[{ key: 1 }, { key: 2 }, { missesKey: 3 }].map(j({ keyExists: sel.exists('key') }));\n// -\u003e [ { keyExists: true }, { keyExists: true }, { keyExists: false } ]\n```\n\n#### `j.sel(selector[, transformationFn])`\n\nThis creates an instance of `Selector` from a string. It may optionally have a transformation function passed into it with the following method signature `function (valueToBeTransformed) { return transformedValue; }`.\n\nExamples of valid selectors are:\n\n```\na-selector\na/deeply/nested/selector\na/deeply/nested/array-item[5]\narray[0][1][2]\n```\n\nIt is also possible to pass in an array of selectors.\n\n### Helpers\n\nOn addition to `sel(selector, transformFn)` there are also special use cases that are already handled by the library.\n\nThese are:\n\n#### `escape(valueToBeEscaped)`\n\nYou can use this if you need to be able to assign a value to the definition of the output and do not want it to select anything.\n\n#### `sel.exists(selector)`\n\nReturns `true` or `false` dependent on whether the value at the end of the selector is not `null` or `undefined`.\n\n#### `sel.first(selectors)`\n\nReturns the first value of the array returned by the selector(s). This is useful when you want the value to be the first value found within given an array of selectors.\n\n#### `sel.isNotEmpty(selector)`\n\nReturns `true` or `false` dependent on whether the object at the end of the selector is empty or not.\n\n#### `sel.defaultsTo(selector, defaultValue)`\n\nReturns either the value found at the selector or the value passed in as the second argument.\n\n## Installation\n```shell\nnpm install [--save] jstruct;\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebinsua%2Fjstruct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebinsua%2Fjstruct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebinsua%2Fjstruct/lists"}