{"id":17436283,"url":"https://github.com/92nqb/bubble-gum-tools","last_synced_at":"2025-04-16T03:40:52.504Z","repository":{"id":57190812,"uuid":"71116396","full_name":"92nqb/bubble-gum-tools","owner":"92nqb","description":"Work with nested objects is easy with a bubble-gum.","archived":false,"fork":false,"pushed_at":"2017-03-29T11:48:55.000Z","size":510,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T19:53:31.082Z","etag":null,"topics":["bubblegum","get","has","js","monorepo","nested-objects","set","tools","utils"],"latest_commit_sha":null,"homepage":"","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/92nqb.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":"2016-10-17T08:30:27.000Z","updated_at":"2023-11-05T11:29:58.000Z","dependencies_parsed_at":"2022-09-15T15:51:11.026Z","dependency_job_id":null,"html_url":"https://github.com/92nqb/bubble-gum-tools","commit_stats":null,"previous_names":["nquicenob/bubble-gum-tools"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/92nqb%2Fbubble-gum-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/92nqb%2Fbubble-gum-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/92nqb%2Fbubble-gum-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/92nqb%2Fbubble-gum-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/92nqb","download_url":"https://codeload.github.com/92nqb/bubble-gum-tools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249191705,"owners_count":21227628,"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":["bubblegum","get","has","js","monorepo","nested-objects","set","tools","utils"],"created_at":"2024-10-17T10:08:08.480Z","updated_at":"2025-04-16T03:40:52.487Z","avatar_url":"https://github.com/92nqb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bubble-gum-tools\n\n[![Coverage Status](https://coveralls.io/repos/github/nquicenob/bubble-gum-tools/badge.svg?branch=master)](https://coveralls.io/github/nquicenob/bubble-gum-tools?branch=master)\n[![Build Status](https://travis-ci.org/nquicenob/bubble-gum-tools.svg?branch=master)](https://travis-ci.org/nquicenob/bubble-gum-tools)\n[![license](https://img.shields.io/github/license/nquicenob/bubble-gum-tools.svg)](https://github.com/nquicenob/bubble-gum-tools/blob/master/LICENSE)\n[![David](https://img.shields.io/david/nquicenob/bubble-gum-tools.svg)](https://david-dm.org/nquicenob/bubble-gum-tools)\n[![npm](https://img.shields.io/npm/v/bubble-gum-tools.svg)](https://www.npmjs.com/package/bubble-gum-tools)\n\nWork with nested objects is easy with a bubble-gum-tool.\n\n# Install\n\nYou can install bubble-gum-tools using npm.\n\n```\nnpm install --save bubble-gum-tools\n```\n\nOr you can also get a [modularized package per each method](https://www.npmjs.com/browse/keyword/bubble-gum-tools-modularized).\n\n# API Reference\n\n## Modules\n\n\u003cdl\u003e\n\u003cdt\u003e\u003ca href=\"#module_bubble-gum-tools\"\u003ebubble-gum-tools\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003c/dd\u003e\n\u003c/dl\u003e\n\n## Typedefs\n\n\u003cdl\u003e\n\u003cdt\u003e\u003ca href=\"#actionCallback\"\u003eactionCallback\u003c/a\u003e ⇒ \u003ccode\u003e*\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eCallback at the end of the loop\u003c/p\u003e\n\u003c/dd\u003e\n\u003c/dl\u003e\n\n\u003ca name=\"module_bubble-gum-tools\"\u003e\u003c/a\u003e\n\n## bubble-gum-tools\n**Example**  \n```javascript\nconst bubbleGumTools = require('bubble-gum-tools');\n\nconst nestedObj = {\n  root: [{\n    foo: 'bar',\n  }],\n};\n\n// get\nconst foo = bubbleGumTools.get(nestedObj, ['root', 0, 'foo']);\nconsole.log(foo);   //  =\u003e 'bar'\n\n// has\nconst existsFoo = bubbleGumTools.has(nestedObj, ['root', 0, 'foo']);\nconsole.log(existsFoo); // =\u003e true\n\n// set\nbubbleGumTools.set(nestedObj, ['root', 0, 'foo'], 'newBar');\nconsole.log(nestedObj);   //  =\u003e { root: [{ foo: 'newBar' }] }\n\n// slice\nconst sObject = bubbleGumTools.slice(nestedObj, [{\n  path: ['root', 0, 'foo'],\n  newPath: ['newFoo'],\n}]);\nconsole.log(sObject);   // =\u003e { newFoo: 'newBar' }\n\n// create\nconst cObject = bubbleGumTools.create(['other-root', 0, 'other-foo'], 'other-bar');\nconsole.log(cObject); // =\u003e { 'other-root': [{ 'other-foo': 'other-bar' }] }\n\n// goto\nconst resultGOTO = bubbleGumTools.goto(['other-root', 0, 'other-foo'], ({ current, key }) =\u003e ({\n  [key]: current,\n}))(cObject);\nconsole.log(resultGOTO); // =\u003e { 'other-foo': 'other-bar' }\n\n```\n\n* [bubble-gum-tools](#module_bubble-gum-tools)\n    * [.create(path, initValue)](#module_bubble-gum-tools.create) ⇒ \u003ccode\u003eObject\u003c/code\u003e \u0026#124; \u003ccode\u003eArray\u003c/code\u003e\n    * [.goto(path, fn)](#module_bubble-gum-tools.goto) ⇒ \u003ccode\u003efunction\u003c/code\u003e\n    * [.get(target, path, [defaultValue])](#module_bubble-gum-tools.get) ⇒ \u003ccode\u003e\\*\u003c/code\u003e\n    * [.has(target, path, [isStrict])](#module_bubble-gum-tools.has) ⇒ \u003ccode\u003eBoolean\u003c/code\u003e\n    * [.set(target, path, valueToSet)](#module_bubble-gum-tools.set)\n    * [.slice(target, config)](#module_bubble-gum-tools.slice) ⇒ \u003ccode\u003eObject\u003c/code\u003e\n\n\u003ca name=\"module_bubble-gum-tools.create\"\u003e\u003c/a\u003e\n\n### bubble-gum-tools.create(path, initValue) ⇒ \u003ccode\u003eObject\u003c/code\u003e \u0026#124; \u003ccode\u003eArray\u003c/code\u003e\nIt creates a new object or an initialized array depending on the input path\n\n**Kind**: static method of \u003ccode\u003e[bubble-gum-tools](#module_bubble-gum-tools)\u003c/code\u003e  \n**Returns**: \u003ccode\u003eObject\u003c/code\u003e \u0026#124; \u003ccode\u003eArray\u003c/code\u003e - output - The new array or new object with the input path structure  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| path | \u003ccode\u003eArray\u003c/code\u003e | Input path with the structure |\n| initValue | \u003ccode\u003e\\*\u003c/code\u003e | Initial value for the end of the input path structure |\n\n**Example**  \n```js\n const create = require('bubble-gum-tools').create;\n\n // create nested arrays\n const nestedArray = create([0, 2, 0], 'bar');\n console.log(nestedArray); // =\u003e [ [ , , [ 'bar' ] ] ]\n\n // create nested objects\n const nestedObject = create(['root', 'foo', 'bar'], 'bar');\n console.log(nestedObject); // =\u003e { root: { foo: { bar: 'bar' } } }\n\n // no defined value\n const noDefaultVal = get(target, ['no', 'defined']);\n console.log(noDefaultVal); // =\u003e undefined\n\n // create both\n const mixed = create([0, 'nested', 'key'], 'value');\n console.log(mixed); // =\u003e [ { nested: { key: 'value' } } ]\n\n```\n\u003ca name=\"module_bubble-gum-tools.goto\"\u003e\u003c/a\u003e\n\n### bubble-gum-tools.goto(path, fn) ⇒ \u003ccode\u003efunction\u003c/code\u003e\nIt receives a input path and a callback(actionCallback), It returns the function **_goto**,\nthe *_goto* function receives a target object or target array,\nwhen the *_goto* is called, this navigates the target object or target array using the input path,\nwhen it reaches the end of the path, *_goto* executs the callback and returns the result\n\n**Kind**: static method of \u003ccode\u003e[bubble-gum-tools](#module_bubble-gum-tools)\u003c/code\u003e  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| path | \u003ccode\u003eArray\u003c/code\u003e | Path to property |\n| fn | \u003ccode\u003e[actionCallback](#actionCallback)\u003c/code\u003e | Callback with the action that will be called at the end of the path |\n\n**Example**  \n```javascript\n\nconst goto = require('bubble-gum-tools').goto;\n\nconst target = {\n  root: {\n    foo: 'bar',\n  },\n};\n\ngoto(['root', 'foo'], (result) =\u003e {\n  const {\n    indexPath,\n    previous,\n    target,\n    current,\n    key,\n  } = result;\n  console.log(indexPath);  // =\u003e  1\n  console.log(previous);  // =\u003e { foo: 'bar' }\n  console.log(target);  // =\u003e { root: { foo: 'bar' }, arr: [ [ [Object] ] ] }\n  console.log(current);  // =\u003e bar\n  console.log(key);  // =\u003e foo\n})(target);\n\nconst result = goto(['root', 'foo'], ({current, key}) =\u003e (current + '-' + key))(target);\nconsole.log(result); // =\u003e bar-foo\n\n```\n\u003ca name=\"module_bubble-gum-tools.get\"\u003e\u003c/a\u003e\n\n### bubble-gum-tools.get(target, path, [defaultValue]) ⇒ \u003ccode\u003e\\*\u003c/code\u003e\nIt gets a property from a nested object or a nested array using an array path\n\n**Kind**: static method of \u003ccode\u003e[bubble-gum-tools](#module_bubble-gum-tools)\u003c/code\u003e  \n**Returns**: \u003ccode\u003e\\*\u003c/code\u003e - propertyValue  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| target | \u003ccode\u003eObject\u003c/code\u003e \u0026#124; \u003ccode\u003eArray\u003c/code\u003e | Target object or target array |\n| path | \u003ccode\u003eArray\u003c/code\u003e | Path to property |\n| [defaultValue] | \u003ccode\u003e\\*\u003c/code\u003e | Value to be returned in case the property does not exist |\n\n**Example**  \n```javascript\n\n const get = require('bubble-gum-tools').get;\n\n const target = {\n   root: {\n     foo: 'bar',\n   },\n   arr: [[\n     ['baz'],\n   ]],\n };\n\n // Working with nested objects\n const bar = get(target, ['root', 'foo']);\n console.log(bar); // =\u003e 'bar'\n\n // Working with nested arrays\n const baz = get(target, ['arr', 0, 0, 0]);\n console.log(baz); // =\u003e 'baz'\n\n // Set a default\n const defaultVal = get(target, ['no', 'defined'], 'default');\n console.log(defaultVal); // =\u003e 'default'\n\n```\n\u003ca name=\"module_bubble-gum-tools.has\"\u003e\u003c/a\u003e\n\n### bubble-gum-tools.has(target, path, [isStrict]) ⇒ \u003ccode\u003eBoolean\u003c/code\u003e\nIt checks if the property exists in a nested object or a nested array using an array path\n\n**Kind**: static method of \u003ccode\u003e[bubble-gum-tools](#module_bubble-gum-tools)\u003c/code\u003e  \n**Returns**: \u003ccode\u003eBoolean\u003c/code\u003e - exists - Returns if the property exists  \n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| target | \u003ccode\u003eObject\u003c/code\u003e \u0026#124; \u003ccode\u003eArray\u003c/code\u003e |  | Target object or target array |\n| path | \u003ccode\u003eArray\u003c/code\u003e |  | Path to property |\n| [isStrict] | \u003ccode\u003eBoolean\u003c/code\u003e | \u003ccode\u003efalse\u003c/code\u003e | is strict |\n\n**Example**  \n```javascript\n\n const has = require('bubble-gum-tools').has;\n\n const target1 = {\n   root: {\n     foo: 'bar',\n   },\n   arr: [[\n     ['baz'],\n   ]],\n };\n\n const existsBar = has(target1, ['root', 'foo']);\n console.log(existsBar); // =\u003e true\n\n const existsBaz = has(target1, ['arr', 0, 0, 0]);\n console.log(existsBaz); // =\u003e true\n\n const noExists = has(target1, ['no', 'defined']);\n console.log(noExists); // =\u003e false\n\n```\n\n**isStrict = false**\n\n```javascript\n\n const has = require('bubble-gum-tools').has;\n\n const target = {\n   root: {\n     zero: 0,\n     null: null,\n     empty: '',\n     false: false,\n   },\n };\n\n const isNotStrict = false;\n const noStrictZero = has(target, ['root', 'zero'], isNotStrict);\n console.log(noStrictZero); // =\u003e false\n const noStrictNull = has(target, ['root', 'null'], isNotStrict);\n console.log(noStrictNull); // =\u003e false\n const noStrictEmpty = has(target, ['root', 'empty'], isNotStrict);\n console.log(noStrictEmpty); // =\u003e false\n const noStrictFalse = has(target, ['root', 'false'], isNotStrict);\n console.log(noStrictFalse); // =\u003e false\n\n```\n\n**isStrict = true**\n\n```javascript\n\n\n const has = require('bubble-gum-tools').has;\n\n const target = {\n   root: {\n     zero: 0,\n     null: null,\n     empty: '',\n     false: false,\n   },\n };\n\n const isStrict = true;\n const strictZero = has(target, ['root', 'zero'], isStrict);\n console.log(strictZero); // =\u003e true\n const strictEmpty = has(target, ['root', 'empty'], isStrict);\n console.log(strictEmpty); // =\u003e true\n const strictNull = has(target, ['root', 'null'], isStrict);\n console.log(strictNull); // =\u003e false\n const strictFalse = has(target, ['root', 'false'], isStrict);\n console.log(strictFalse); // =\u003e false\n\n```\n\u003ca name=\"module_bubble-gum-tools.set\"\u003e\u003c/a\u003e\n\n### bubble-gum-tools.set(target, path, valueToSet)\nIt sets a new value in a nested object or a nested array using an array path, if the path does not exist create this\n\n**Kind**: static method of \u003ccode\u003e[bubble-gum-tools](#module_bubble-gum-tools)\u003c/code\u003e  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| target | \u003ccode\u003eObject\u003c/code\u003e \u0026#124; \u003ccode\u003eArray\u003c/code\u003e | Target object or target array |\n| path | \u003ccode\u003eArray\u003c/code\u003e | Path to property |\n| valueToSet | \u003ccode\u003e\\*\u003c/code\u003e | Value to set in target |\n\n**Example**  \n```javascript\n\nconst set = require('bubble-gum-tools').set;\n\nconst target = {\n  root: {\n    foo: 'bar',\n  },\n  arr: [[\n    ['baz'],\n  ]],\n};\n\nset(target, ['root', 'foo'], 'newbar');\nconsole.log(target.root.foo); // =\u003e 'newbar'\n\nset(target, ['arr', 0, 0, 0], 'newbaz');\nconsole.log(target.arr[0][0][0]); // =\u003e 'newbaz'\n\nset(target, ['root', 'foo2'], 'foo2');\nconsole.log(target.root.foo2); // =\u003e 'foo2'\n\nset(target, ['arr', 0, 0, 1], 'newbaz2');\nconsole.log(target.arr[0][0][1]); // =\u003e 'newbaz2'\n\n```\n\u003ca name=\"module_bubble-gum-tools.slice\"\u003e\u003c/a\u003e\n\n### bubble-gum-tools.slice(target, config) ⇒ \u003ccode\u003eObject\u003c/code\u003e\nIt slices a object or an array generating a new object or a new array\n\n**Kind**: static method of \u003ccode\u003e[bubble-gum-tools](#module_bubble-gum-tools)\u003c/code\u003e  \n**Returns**: \u003ccode\u003eObject\u003c/code\u003e - slicedObject - New object or new array with sliced values  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| target | \u003ccode\u003eObject\u003c/code\u003e \u0026#124; \u003ccode\u003eArray\u003c/code\u003e | Target object or target array |\n| config | \u003ccode\u003eArray.Object\u003c/code\u003e | Array with the configuration of the slice |\n| config[].path | \u003ccode\u003eArray\u003c/code\u003e | Path to the property to be sliced |\n| config[].newPath | \u003ccode\u003eArray\u003c/code\u003e | Path to sets a new value in the slicedObject, if this is not defined, this will have the same value as the config[].path |\n\n**Example**  \n```javascript\n\nconst slice = require('bubble-gum-tools').slice;\n\nconst target = {\n  root: { foo: 'bar' },\n  arr: [[['baz']]],\n};\n\nconst sliced1 = slice(target, [{\n  path: ['root', 'foo']\n}]);\nconsole.log(sliced1); // =\u003e { root: { foo: 'bar' } }\n\nconst sliced2 = slice(target, [{\n  path: ['root', 'foo'],\n  newPath: ['bar'],\n}]);\nconsole.log(sliced2); // =\u003e { bar: 'bar' }\n\nconst sliced3 = slice(target, [{\n  path: ['root', 'foo'],\n  newPath: [0],\n}]);\nconsole.log(sliced3); // =\u003e { '0': 'bar' }\n\nconst sliced4 = slice(target, [{\n  path: ['arr', 0, 0, 0],\n  newPath: ['baz'],\n}, {\n  path: ['root', 'foo'],\n  newPath: ['bar'],\n}]);\nconsole.log(sliced4); // =\u003e { baz: 'baz', bar: 'bar' }\n\n```\n\u003ca name=\"actionCallback\"\u003e\u003c/a\u003e\n\n## actionCallback ⇒ \u003ccode\u003e\\*\u003c/code\u003e\nCallback at the end of the loop\n\n**Kind**: global typedef  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| options | \u003ccode\u003eObject\u003c/code\u003e | Values in the end of the path |\n| options.indexPath | \u003ccode\u003eNumber\u003c/code\u003e | Current index in the array path |\n| options.target | \u003ccode\u003eObject\u003c/code\u003e \u0026#124; \u003ccode\u003eArray\u003c/code\u003e | Target object or target array |\n| [options.current] | \u003ccode\u003e\\*\u003c/code\u003e | Current value in target object |\n| [options.key] | \u003ccode\u003e\\*\u003c/code\u003e | Current value in the path |\n| [options.previous] | \u003ccode\u003e\\*\u003c/code\u003e | Previous value in target object |\n\n\n# TODOS\n\n* [ ] Add method to compare objects in depth\n* [ ] Add method to clone objects in depth\n\n# License\n\nMIT @ Nicolas Quiceno\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F92nqb%2Fbubble-gum-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F92nqb%2Fbubble-gum-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F92nqb%2Fbubble-gum-tools/lists"}