### Installing object-path with Bower Source: https://github.com/mariocasciaro/object-path/blob/master/README.md This command installs the `object-path` library using Bower, a package manager for the web, and saves it as a dependency. This method is typically used for front-end web development. ```bash bower install object-path --save ``` -------------------------------- ### Installing object-path with npm Source: https://github.com/mariocasciaro/object-path/blob/master/README.md This command installs the `object-path` library as a dependency in a Node.js project using npm, saving it to the `package.json` file. This is the recommended installation method for Node.js applications. ```bash npm install object-path --save ``` -------------------------------- ### Installing object-path TypeScript typings Source: https://github.com/mariocasciaro/object-path/blob/master/README.md This command installs the TypeScript declaration files for the `object-path` library using the `typings` tool, saving them as a dependency. These typings provide type definitions for TypeScript projects, enabling better autocompletion and type checking. ```bash typings install --save dt~object-path ``` -------------------------------- ### Binding Objects for Chained Operations with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Demonstrates how to bind an object to the `object-path` instance, allowing subsequent method calls (like `get`, `set`, `del`, `has`) to operate directly on the bound object without needing to pass it as the first argument. This enables a more fluent API style. ```javascript //bind object var model = objectPath({ a: { b: "d", c: ["e", "f"] } }); //now any method from above is supported directly w/o passing an object model.get("a.b"); //returns "d" model.get(["a.c.b"], "DEFAULT"); //returns "DEFAULT" model.del("a.b"); // obj.a.b is now undefined model.has("a.b"); // false ``` -------------------------------- ### Getting Deep Properties with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Demonstrates how to retrieve values from deeply nested properties using `objectPath.get()`. It supports both string path notation and array path notation, handles unicode and dot-in-key properties, and allows specifying a default return value for non-existent paths. ```javascript //get deep property objectPath.get(obj, "a.b"); //returns "d" objectPath.get(obj, ["a", "dot.dot"]); //returns "key" objectPath.get(obj, 'a.\u1200'); //returns "unicode key" //works also with arrays objectPath.get(obj, "a.c.1"); //returns "f" objectPath.get(obj, ["a","c","1"]); //returns "f" //can return a default value with get objectPath.get(obj, ["a.c.b"], "DEFAULT"); //returns "DEFAULT", since a.c.b path doesn't exists, if omitted, returns undefined ``` -------------------------------- ### Accessing Inherited Properties with Configured object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md This snippet demonstrates how to use an `object-path` instance configured to include inherited properties (e.g., `objectPath.withInheritedProps`). When using this instance, `get()` will successfully retrieve values from the prototype chain, and `set()` will modify the property on the prototype if it's inherited. ```javascript var proto = { notOwn: {prop: 'a'} } var obj = Object.create(proto); //This will return 'a' objectPath.withInheritedProps.get(obj, 'notOwn.prop'); //This will set proto.notOwn.prop to 'b' objectPath.set(obj, 'notOwn.prop', 'b'); ``` -------------------------------- ### Initializing Object and object-path Library in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md This snippet initializes a sample JavaScript object with nested properties, including special characters and dot-notation keys, and imports the `object-path` library for subsequent operations. ```javascript var obj = { a: { b: "d", c: ["e", "f"], '\u1200': 'unicode key', 'dot.dot': 'key' } }; var objectPath = require("object-path"); ``` -------------------------------- ### Configuring object-path for Inherited Properties (Create) in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Shows how to create a new `object-path` instance configured to include inherited properties. By passing `{includeInheritedProps: true}` to `objectPath.create()`, the new instance will traverse the prototype chain when accessing properties. ```javascript var objectPath = require("object-path"); var objectPathWithInheritedProps = objectPath.create({includeInheritedProps: true}) ``` -------------------------------- ### Using Pre-configured object-path for Inherited Properties in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Demonstrates how to access the `objectPath.withInheritedProps` instance, which is a pre-configured `object-path` instance designed to handle inherited properties without needing explicit creation. ```javascript var objectPath = require("object-path"); var objectPathWithInheritedProps = objectPath.withInheritedProps ``` -------------------------------- ### Ensuring Path Existence with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Demonstrates `objectPath.ensureExists()`, which verifies if a path exists within an object. If the path does not exist, it sets the path to a provided default value; otherwise, it returns the existing value at that path. ```javascript //ensure a path exists (if it doesn't, set the default value you provide) objectPath.ensureExists(obj, "a.k.1", "DEFAULT"); var oldVal = objectPath.ensureExists(obj, "a.b", "DEFAULT"); // oldval === "d" ``` -------------------------------- ### Pushing into Arrays with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Shows `objectPath.push()`, which appends a value to an array at the specified path. If the path does not exist, it will create necessary intermediate objects or arrays to ensure the array can be pushed into. ```javascript //push into arrays (and create intermediate objects/arrays) objectPath.push(obj, "a.k", "o"); ``` -------------------------------- ### Setting Deep Properties with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Demonstrates `objectPath.set()`, which assigns a value to a specified path. This method is capable of creating intermediate objects or arrays if the path does not fully exist, ensuring the target path is valid before setting the value. ```javascript //set objectPath.set(obj, "a.h", "m"); // or objectPath.set(obj, ["a","h"], "m"); objectPath.get(obj, "a.h"); //returns "m" //set will create intermediate object/arrays objectPath.set(obj, "a.j.0.f", "m"); ``` -------------------------------- ### Handling Inherited Properties (Default) with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md This snippet illustrates `object-path`'s default behavior regarding inherited properties. By default, `objectPath.get()` will return `undefined` for inherited properties, and `objectPath.set()` will create an own property on the instance rather than modifying the prototype. ```javascript var proto = { notOwn: {prop: 'a'} } var obj = Object.create(proto); //This will return undefined (or the default value you specified), because notOwn is //an inherited property objectPath.get(obj, 'notOwn.prop'); //This will set the property on the obj instance and not the prototype. //In other words proto.notOwn.prop === 'a' and obj.notOwn.prop === 'b' objectPath.set(obj, 'notOwn.prop', 'b'); ``` -------------------------------- ### Checking Path Existence with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Shows `objectPath.has()`, which checks for the existence of a property at a given path. It returns `true` if the path exists and `false` otherwise, supporting both string and array path notations. ```javascript //tests path existence objectPath.has(obj, "a.b"); // true objectPath.has(obj, ["a","d"]); // false ``` -------------------------------- ### Inserting into Arrays with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Illustrates `objectPath.insert()`, which adds a value into an array at a specific index. This method modifies the array in place, shifting existing elements to accommodate the new value. ```javascript //will insert values in array objectPath.insert(obj, "a.c", "m", 1); // obj.a.c = ["e", "m", "f"] ``` -------------------------------- ### Emptying Properties with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Shows how `objectPath.empty()` clears the value at a specified path without deleting the property itself. It sets strings to empty string, arrays to empty array, and objects to empty object, retaining references for objects and arrays. ```javascript //empty a given path (but do not delete it) depending on their type,so it retains reference to objects and arrays. //functions that are not inherited from prototype are set to null. //object instances are considered objects and just own property names are deleted objectPath.empty(obj, 'a.b'); // obj.a.b is now '' objectPath.empty(obj, 'a.c'); // obj.a.c is now [] objectPath.empty(obj, 'a'); // obj.a is now {} ``` -------------------------------- ### Coalescing Values with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Illustrates the `objectPath.coalesce()` method, which returns the first non-undefined value found among a list of specified paths. If all paths resolve to `undefined`, it returns the provided default value. ```javascript //get the first non-undefined value objectPath.coalesce(obj, ['a.z', 'a.d', ['a','b']], 'default'); ``` -------------------------------- ### Deleting Properties with object-path in JavaScript Source: https://github.com/mariocasciaro/object-path/blob/master/README.md Illustrates `objectPath.del()`, which removes a property at the specified path from an object. After deletion, the property at that path will be `undefined`. ```javascript //deletes a path objectPath.del(obj, "a.b"); // obj.a.b is now undefined objectPath.del(obj, ["a","c",0]); // obj.a.c is now ['f'] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.