{"id":16189290,"url":"https://github.com/caolan/copykitten","last_synced_at":"2025-03-19T03:30:52.023Z","repository":{"id":57207344,"uuid":"58857693","full_name":"caolan/copykitten","owner":"caolan","description":"Tiny immutable JSON data structures","archived":false,"fork":false,"pushed_at":"2017-11-17T18:13:48.000Z","size":104,"stargazers_count":41,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T15:04:40.870Z","etag":null,"topics":[],"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/caolan.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":"2016-05-15T11:16:52.000Z","updated_at":"2024-12-25T13:28:27.000Z","dependencies_parsed_at":"2022-09-11T05:30:28.827Z","dependency_job_id":null,"html_url":"https://github.com/caolan/copykitten","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caolan%2Fcopykitten","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caolan%2Fcopykitten/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caolan%2Fcopykitten/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caolan%2Fcopykitten/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/caolan","download_url":"https://codeload.github.com/caolan/copykitten/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243965774,"owners_count":20375917,"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-10T07:34:52.603Z","updated_at":"2025-03-19T03:30:51.688Z","avatar_url":"https://github.com/caolan.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# copykitten.js\n\n```\n    /\\/\\\n( =( ^-^)=\n \\(  ⊃C⊃\n      Copy Kitten\n```\n\n_Tiny immutable JSON data structures (\u003c 1kb gzip)_\n\nThis is a minimal library, designed specifically for\nJSON-compatible values: Strings, Numbers, Booleans, null, Objects, and Arrays.\nFor a wider selection of immutable data structures with less naive\nimplementations, see [Immutable.js][immutable-js].\n\nCopy Kitten manipulates JSON data and returns an updated copy; the original data is\npreserved. A reference to a Copy Kitten result will always return the same\nvalue. Use this to write easy-to-understand code and optimise\ntop-down rendering (e.g. [Redux][redux]).\n\n\n## Data conversion\n\n```javascript\n// JSON value -\u003e immutable\nvar immutable = copykitten.toImmutable({example: 'value'})\n\n// Immutable value -\u003e JSON string\nJSON.stringify(immutable)\n\n// Immutable value -\u003e JSON object\nimmutable.toJSON();\n```\n\n## API\n\n* [toImmutable](#toImmutable)\n* [isImmutable](#isImmutable)\n* [FrozenObject](#FrozenObject)\n  * [set(key, value)](#FrozenObject_set)\n  * [remove(key)](#FrozenObject_remove)\n  * [merge(props)](#FrozenObject_merge)\n  * [deepMerge(props)](#FrozenObject_deepMerge)\n  * [update(f)](#FrozenObject_update)\n  * [thaw()](#FrozenObject_thaw)\n  * [toJSON()](#FrozenObject_toJSON)\n* [FrozenArray](#FrozenArray)\n  * [push(element1, ..., elementN)](#FrozenArray_push)\n  * [pop()](#FrozenArray_pop)\n  * [unshift(element1, ..., elementN)](#FrozenArray_unshift)\n  * [shift()](#FrozenArray_shift)\n  * [sort()](#FrozenArray_sort)\n  * [splice(start, deleteCount, item1, item2, ...)](#FrozenArray_splice)\n  * [slice(begin, end)](#FrozenArray_slice)\n  * [every(callback)](#FrozenArray_every)\n  * [filter(callback)](#FrozenArray_filter)\n  * [forEach(callback)](#FrozenArray_forEach)\n  * [indexOf(searchElement)](#FrozenArray_indexOf)\n  * [join()](#FrozenArray_join)\n  * [lastIndexOf(searchElement)](#FrozenArray_lastIndexOf)\n  * [map(callback)](#FrozenArray_map)\n  * [reduce(callback)](#FrozenArray_reduce)\n  * [reduceRight(callback)](#FrozenArray_reduceRight)\n  * [some(callback)](#FrozenArray_some)\n\n\u003ca name=\"toImmutable\"\u003e\u003c/a\u003e\n\n### toImmutable(value)\n\nConverts `value` to an immutable value. Basic types (string, number,\nboolean, null) are untouched, objects and arrays are converted to\nFrozenObject and FrozenArray types respectively.\n\n```javascript\nvar data = copykitten.toImmutable({\n    name: 'Kitten'\n});\n```\n\n\u003ca name=\"isImmutable\"\u003e\u003c/a\u003e\n\n### isImmutable(value)\n\nReturns true for string, number, boolean, null, undefined,\nFrozenObject and FrozenArray. Otherwise returns false.\n\n```javascript\ncopykitten.isImmutable(\"string\")  // true\ncopykitten.isImmutable(1234)  // true\ncopykitten.isImmutable(true)  // true\ncopykitten.isImmutable(null)  // true\n\ncopykitten.isImmutable({name: 'Kitten'})  // false\n\nvar data = copykitten.toImmutable({name: 'Kitten'});\ncopykitten.isImmutable(data)  // true\n```\n\n\u003ca name=\"FrozenObject\"\u003e\u003c/a\u003e\n\n### FrozenObject\n\nRepresents a JSON-style object (e.g. {hello: 'world'}) which has been\nfrozen and made immutable.\n\nThis constructor should not be called directly.\n\n\u003ca name=\"FrozenObject_set\"\u003e\u003c/a\u003e\n\n#### set(key, value)\n\nReturns a new copy of the FrozenObject with the property `key` set to\n`value`. The value is first converted to an immutable (including deep\nreferences) before being set on the newly returned FrozenObject.\n\n```javascript\nvar a = copykitten.toImmutable({name: 'Kitten'});\nvar b = data.set('status', 'sleepy');\n\n// a is now {name: 'Kitten'}\n// b is now {name: 'Kitten', status: 'sleepy'}\n```\n\n\u003ca name=\"FrozenObject_remove\"\u003e\u003c/a\u003e\n\n#### remove(key)\n\nReturns a new copy of the FrozenObject with the property `key`\nremoved.\n\n```javascript\nvar a = copykitten.toImmutable({name: 'Kitten', toy: 'ball'});\nvar b = a.remove('toy');\n\n// a is now {name: 'Kitten', toy: 'ball'}\n// b is now {name: 'Kitten'}\n```\n\n\u003ca name=\"FrozenObject_merge\"\u003e\u003c/a\u003e\n\n#### merge(props)\n\nReturns a new copy of the FrozenObject including the properties and\nvalues from the `props` object. Any values defined on `props` will be\nconverted to immutables first (including deep references).\n\n```javascript\nvar a = copykitten.toImmutable({name: 'Kitten'});\nvar b = a.merge({status: 'hungry'});\n\n// a is now {name: 'Kitten'}\n// b is now {name: 'Kitten', status: 'hungry'}\n```\n\n\u003ca name=\"FrozenObject_deepMerge\"\u003e\u003c/a\u003e\n\n#### deepMerge(props)\n\nLike [merge(props)](#FrozenObject_merge) but will recursively merge\nnested objects.\n\n```javascript\nvar a = copykitten.toImmutable({animal: {name: 'Kitten'}});\nvar b = a.deepMerge({animal: {status: 'hungry'}});\n\n// a is now {animal: {name: 'Kitten'}}\n// b is now {animal: {name: 'Kitten', status: 'hungry'}}\n```\n\n\u003ca name=\"FrozenObject_update\"\u003e\u003c/a\u003e\n\n#### update(f)\n\nCalls the function `f` with a mutable copy of the current\nFrozenObject, then returns an immutable version including any\nmodifications made by `f`.\n\n```javascript\nvar a = copykitten.toImmutable({name: 'Kitten'});\nvar b = a.update(function (obj) {\n    delete obj.name;\n    obj.type = 'animal';\n});\n\n// a is now {name: 'Kitten'}\n// b is now {type: 'animal'}\n```\n\nOnly the current object is mutable, any nested properties will remain\nimmutable while `f` is being called.\n\n\u003ca name=\"FrozenObject_thaw\"\u003e\u003c/a\u003e\n\n#### thaw()\n\nReturns a mutable JSON data type for the current object. Any nested\nproperties will remain immutable.\n\n```javascript\nvar obj = copykitten.toImmutable({name: 'Kitten'});\nobj.thaw()  // =\u003e {name: 'Kitten'}\n```\n\n\u003ca name=\"FrozenObject_toJSON\"\u003e\u003c/a\u003e\n\n#### toJSON()\n\nConverts the current object and all nested properties to their mutable\nJSON data type representations.\n\n```javascript\nvar obj = copykitten.toImmutable({name: 'Kitten'});\nobj.toJSON()  // =\u003e {name: 'Kitten'}\n```\n\n\u003ca name=\"FrozenArray\"\u003e\u003c/a\u003e\n\n### FrozenArray\n\nFrozenArray extends FrozenObject. In addition to the FrozenObject\nmethods, it provides immutable implementations of most Array methods.\nSome descriptions borrowed from the\n[MDN Array documentation][mdn-arra].\n\nThis constructor should not be called directly.\n\n\u003ca name=\"FrozenArray_push\"\u003e\u003c/a\u003e\n\n#### push(element1, ..., elementN)\n\nReturns a new FrozenArray with `value` appended.\n\n```javascript\nvar a = copykitten.toImmutable(['yarn', 'ball']);\nvar b = a.push('mouse');\n\n// a is now ['yarn', 'ball']\n// b is now ['yarn', 'ball', 'mouse']\n```\n\n\u003ca name=\"FrozenArray_pop\"\u003e\u003c/a\u003e\n\n#### pop()\n\nReturns a new FrozenArray with the last item removed.\n\n```javascript\nvar a = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\nvar b = a.pop();\n\n// a is now ['yarn', 'ball', 'mouse']\n// b is now ['yarn', 'ball']\n```\n\n__Note:__ does not return the popped element, just the updated array.\n\n\u003ca name=\"FrozenArray_unshift\"\u003e\u003c/a\u003e\n\n#### unshift([element1[, ...[, elementN]]])\n\nReturns a new FrozenArray with `value` prepended.\n\n```javascript\nvar a = copykitten.toImmutable(['yarn', 'ball']);\nvar b = a.unshift('mouse');\n\n// a is now ['yarn', 'ball']\n// b is now ['mouse', 'yarn', 'ball']\n```\n\n\u003ca name=\"FrozenArray_shift\"\u003e\u003c/a\u003e\n\n#### shift()\n\nReturns a new FrozenArray with the first item removed.\n\n```javascript\nvar a = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\nvar b = a.shift();\n\n// a is now ['yarn', 'ball', 'mouse']\n// b is now ['ball', 'mouse']\n```\n\n__Note:__ does not return the shifted element, just the updated array.\n\n\u003ca name=\"FrozenArray_sort\"\u003e\u003c/a\u003e\n\n#### sort([compareFunction])\n\nReturns a new, sorted FrozenArray.\n\n```javascript\nvar a = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\nvar b = a.sort();\n\n// a is now ['yarn', 'ball', 'mouse']\n// b is now ['ball', 'mouse', 'yarn']\n```\n\n\u003ca name=\"FrozenArray_splice\"\u003e\u003c/a\u003e\n\n#### splice(start, deleteCount[, item1[, item2[, ...]]])\n\nWorks as the Array.splice method. Returns a new FrozenArray updated by\nadding and/or removing elements from the original.\n\n```javascript\nvar a = copykitten.toImmutable(['yarn', 'ball']);\nvar b = a.splice(1, 1, 'mouse');\n\n// a is now ['yarn', 'ball']\n// b is now ['yarn', 'mouse']\n```\n\n\u003ca name=\"FrozenArray_slice\"\u003e\u003c/a\u003e\n\n#### slice([begin[, end]])\n\nReturns a new FrozenArray including a portion of the original.\n\n```javascript\nvar a = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\nvar b = a.slice(1)\n\n// a is now ['yarn', 'ball', 'mouse']\n// b is now ['ball', 'mouse']\n```\n\n\u003ca name=\"FrozenArray_every\"\u003e\u003c/a\u003e\n\n#### every(callback[, thisArg])\n\nTests whether all elements in the FrozenArray pass the test\nimplemented by the provided function. Returns boolean.\n\n```javascript\nvar toys = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\nvar small = toys.every(function (toy) {\n    return toy.length \u003c 10;\n});\n\n// small == true\n```\n\n\u003ca name=\"FrozenArray_filter\"\u003e\u003c/a\u003e\n\n#### filter(callback[, thisArg])\n\nCreates a new FrozenArray with all elements that pass the test implemented\nby the provided function.\n\n```javascript\nvar toys = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\nvar tiny = toys.filter(function (toy) {\n    return toy.length \u003c 4;\n});\n\n// tiny is now ['ball']\n```\n\n\u003ca name=\"FrozenArray_forEach\"\u003e\u003c/a\u003e\n\n#### forEach(callback[, thisArg])\n\nExecutes a provided function once per FrozenArray element.\n\n```javascript\nvar toys = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\n\ntoys.forEach(function (toy) {\n    console.log(toy);\n});\n```\n\n\u003ca name=\"FrozenArray_indexOf\"\u003e\u003c/a\u003e\n\n#### indexOf(searchElement[, fromIndex = 0])\n\nReturns the first index at which a given element can be found in the\nFrozenArray, or -1 if it is not present.\n\n```javascript\nvar toys = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\ntoys.indexOf('ball');  // =\u003e 1\n```\n\n\u003ca name=\"FrozenArray_join\"\u003e\u003c/a\u003e\n\n#### join([separator = ','])\n\nJoins all elements of a FrozenArray into a string.\n\n```javascript\nvar toys = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\ntoys.join(' and ');  // =\u003e 'yarn and ball and mouse'\n```\n\n\u003ca name=\"FrozenArray_lastIndexOf\"\u003e\u003c/a\u003e\n\n#### lastIndexOf(searchElement[, fromIndex = arr.length - 1])\n\nReturns the last index at which a given element can be found in the\nFrozenArray, or -1 if it is not present. The FrozenArray is searched\nbackwards, starting at fromIndex.\n\n```javascript\nvar toys = copykitten.toImmutable(['yarn', 'ball', 'mouse', 'ball']);\ntoys.lastIndexOf('ball');  // =\u003e 3\n```\n\n\u003ca name=\"FrozenArray_map\"\u003e\u003c/a\u003e\n\n#### map(callback[, thisArg])\n\nCreates a new FrozenArray with the results of calling a provided\nfunction on every element in this FrozenArray.\n\n```javascript\nvar toys = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\nvar caps = toys.map(function (toy) {\n    return toy.toUpperCase();\n});\n\n// caps is now ['YARN', 'BALL', 'MOUSE']\n```\n\n\u003ca name=\"FrozenArray_reduce\"\u003e\u003c/a\u003e\n\n#### reduce(callback[, initialValue])\n\nApplies a function against an accumulator and each value of the\nFrozenArray (from left-to-right) to reduce it to a single value.\n\n```javascript\nvar toys = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\nvar size = toys.reduce(function (count, toy) {\n    return count + toy.length;\n}, 0);\n\n// size is now 13\n```\n\n\u003ca name=\"FrozenArray_reduceRight\"\u003e\u003c/a\u003e\n\n#### reduceRight(callback[, initialValue])\n\nApplies a function against an accumulator and each value of the\nFrozenArray (from right-to-left) has to reduce it to a single value.\n\n```javascript\nvar toys = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\nvar smallest = toys.reduceRight(function (min, toy) {\n    if (toy.length \u003c min.length) {\n        return toy;\n    }\n    return min;\n});\n\n// smallest is now 'ball'\n```\n\n\u003ca name=\"FrozenArray_some\"\u003e\u003c/a\u003e\n\n#### some(callback[, thisArg])\n\nTests whether some element in the FrozenArray passes the test\nimplemented by the provided function.\n\n```javascript\nvar toys = copykitten.toImmutable(['yarn', 'ball', 'mouse']);\nvar got_ball = toys.some(function (toy) {\n    return toy === 'ball';\n});\n\n// got_ball == true\n```\n\n\n[immutable-js]: http://facebook.github.io/immutable-js/\n[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array\n[redux]: http://redux.js.org\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaolan%2Fcopykitten","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcaolan%2Fcopykitten","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaolan%2Fcopykitten/lists"}