{"id":15645281,"url":"https://github.com/jonschlinkert/cache-base","last_synced_at":"2025-04-05T02:11:09.353Z","repository":{"id":23335909,"uuid":"26696331","full_name":"jonschlinkert/cache-base","owner":"jonschlinkert","description":"Basic object store with methods like get/set/extend/omit","archived":false,"fork":false,"pushed_at":"2022-04-01T11:09:38.000Z","size":93,"stargazers_count":56,"open_issues_count":5,"forks_count":19,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-29T12:59:31.486Z","etag":null,"topics":["cache","config","data","dot-notation","emit","extend","inherit","javascript","node","nodejs","object","store"],"latest_commit_sha":null,"homepage":null,"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/jonschlinkert.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2014-11-15T22:33:01.000Z","updated_at":"2024-10-04T16:16:36.000Z","dependencies_parsed_at":"2022-07-27T03:47:29.926Z","dependency_job_id":null,"html_url":"https://github.com/jonschlinkert/cache-base","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fcache-base","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fcache-base/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fcache-base/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fcache-base/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonschlinkert","download_url":"https://codeload.github.com/jonschlinkert/cache-base/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276189,"owners_count":20912288,"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":["cache","config","data","dot-notation","emit","extend","inherit","javascript","node","nodejs","object","store"],"created_at":"2024-10-03T12:05:42.360Z","updated_at":"2025-04-05T02:11:09.334Z","avatar_url":"https://github.com/jonschlinkert.png","language":"JavaScript","readme":"# cache-base [![NPM version](https://img.shields.io/npm/v/cache-base.svg?style=flat)](https://www.npmjs.com/package/cache-base) [![NPM monthly downloads](https://img.shields.io/npm/dm/cache-base.svg?style=flat)](https://npmjs.org/package/cache-base) [![NPM total downloads](https://img.shields.io/npm/dt/cache-base.svg?style=flat)](https://npmjs.org/package/cache-base) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/cache-base.svg?style=flat\u0026label=Travis)](https://travis-ci.org/jonschlinkert/cache-base)\n\n\u003e Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.\n\nPlease consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.\n\n- [Install](#install)\n- [Quickstart](#quickstart)\n- [API](#api)\n- [Usage examples](#usage-examples)\n- [About](#about)\n\n_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save cache-base\n```\n\n## Quickstart\n\n```js\nconst CacheBase = require('cache-base');\nconst app = new CacheBase();\n\napp.set('a.b', 'c');\n\nconsole.log(app.cache.a);    //=\u003e { b: 'c' }\nconsole.log(app.cache.a.b);  //=\u003e 'c'\n\nconsole.log(app.get('a'));   //=\u003e { b: 'c' }\nconsole.log(app.get('a.b')); //=\u003e 'c'\n```\n\nMore [usage examples](#usage-examples) below.\n\n## API\n\n**Params**\n\n* `prop` **{String|Object}**: (optional) Property name to use for the cache, or the object to initialize with.\n* `cache` **{Object}**: (optional) An object to initialize with.\n\n**Example**\n\n```js\nconst app = new CacheBase();\n```\n\n### [.set](index.js#L65)\n\nAssign `value` to `key`. Also emits `set` with the key and value.\n\n**Params**\n\n* `key` **{String|Array}**: The name of the property to set. Dot-notation may be used to set nested properties.\n* `value` **{any}**\n* `returns` **{Object}**: Returns the instance for chaining.\n\n**Events**\n\n* `emits`: `set` with `key` and `value` as arguments.\n\n**Example**\n\n```js\napp.on('set', function(key, val) {\n  // do something when `set` is emitted\n});\n\napp.set('admin', true);\n\n// also takes an object or an array of objects\napp.set({ name: 'Brian' });\napp.set([{ foo: 'bar' }, { baz: 'quux' }]);\nconsole.log(app);\n//=\u003e { name: 'Brian', foo: 'bar', baz: 'quux' }\n```\n\n### [.get](index.js#L90)\n\nReturn the value of `key`.\n\n**Params**\n\n* `key` **{String|Array}**: The name of the property to get. Dot-notation may be used to set nested properties.\n* `returns` **{any}**: Returns the value of `key`\n\n**Events**\n\n* `emits`: `get` with `key` and `value` as arguments.\n\n**Example**\n\n```js\napp.set('a.b.c', 'd');\napp.get('a.b');\n//=\u003e { c: 'd' }\n```\n\n### [.prime](index.js#L120)\n\nCreate a property on the cache with the given `value` only if it doesn't already exist.\n\n**Params**\n\n* `key` **{String}**: Property name or object path notation.\n* `val` **{any}**\n* `returns` **{Object}**: Returns the instance for chaining.\n\n**Example**\n\n```js\nconsole.log(app.cache); //=\u003e {}\napp.set('one', { foo: 'bar' });\napp.prime('one', { a: 'b' });\napp.prime('two', { c: 'd' });\nconsole.log(app.cache.one); //=\u003e { foo: 'bar' }\nconsole.log(app.cache.two); //=\u003e { c: 'd' }\n```\n\n### [.default](index.js#L162)\n\nSet a default value to be used when `.get()` is called and the value is not defined on the cache. Returns a value from the defaults when only a key is passed.\n\n**Params**\n\n* `key` **{String|Array}**: The name of the property to set. Dot-notation may be used to set nested properties.\n* `value` **{any}**: (optional) The value to set on the defaults object.\n* `returns` **{Object}**: Returns the instance for chaining.\n\n**Example**\n\n```js\napp.set('foo', 'xxx');\napp.default('foo', 'one');\napp.default('bar', 'two');\napp.default('baz', 'three');\napp.set('baz', 'zzz');\n\nconsole.log(app.get('foo'));\n//=\u003e 'xxx'\n\nconsole.log(app.get('bar'));\n//=\u003e 'two'\n\nconsole.log(app.get('baz'));\n//=\u003e 'zzz'\n\nconsole.log(app);\n// CacheBase {\n//   cache: { foo: 'xxx', bar: 'two', baz: 'zzz' },\n//   defaults: { foo: 'one', bar: 'two', baz: 'three' } }\n```\n\n### [.union](index.js#L199)\n\nSet an array of unique values on cache `key`.\n\n**Params**\n\n* `key` **{String|Array}**: The name of the property to union. Dot-notation may be used to set nested properties.\n* `value` **{any}**\n* `returns` **{Object}**: Returns the instance for chaining.\n\n**Example**\n\n```js\napp.union('a.b.c', 'foo');\napp.union('a.b.c', 'bar');\napp.union('a.b.c', ['bar', 'baz']);\nconsole.log(app.get('a'));\n//=\u003e { b: { c: ['foo', 'bar', 'baz'] } }\n```\n\n### [.has](index.js#L223)\n\nReturn true if the value of property `key` is not `undefined`.\n\n**Params**\n\n* `key` **{String|Array}**: The name of the property to check. Dot-notation may be used to set nested properties.\n* `returns` **{Boolean}**\n\n**Example**\n\n```js\napp.set('foo', true);\napp.set('baz', null);\napp.set('bar', undefined);\n\napp.has('foo'); //=\u003e true\napp.has('bar'); //=\u003e true\napp.has('baz'); //=\u003e false\n```\n\n### [.hasOwn](index.js#L253)\n\nReturns true if the specified property is an own (not inherited) property. Similar to [.has()](#has), but returns true if the key exists, even if the value is `undefined`.\n\n**Params**\n\n* `key` **{String}**\n* `returns` **{Boolean}**: Returns true if object `key` exists. Dot-notation may be used to set nested properties.\n\n**Example**\n\n```js\napp.set('a.b.c', 'd');\napp.set('x', false);\napp.set('y', null);\napp.set('z', undefined);\n\napp.hasOwn('a');      //=\u003e true\napp.hasOwn('b');      //=\u003e true\napp.hasOwn('c');      //=\u003e true\napp.hasOwn('a.b.c');  //=\u003e true\napp.hasOwn('x');      //=\u003e true\napp.hasOwn('y');      //=\u003e true\napp.hasOwn('z');      //=\u003e true\napp.hasOwn('lslsls'); //=\u003e false\n```\n\n### [.del](index.js#L278)\n\nDelete one or more properties from the instance.\n\n**Params**\n\n* `key` **{String|Array}**: The name of the property to delete. Dot-notation may be used to set nested properties.\n* `returns` **{Object}**: Returns the instance for chaining.\n\n**Events**\n\n* `emits`: `del` with the `key` as the only argument.\n\n**Example**\n\n```js\n// setup a listener to update a property with a default\n// value when it's deleted by the user\napp.on('del', key =\u003e app.set(key, app.default(key)));\n\napp.del(); // delete all properties on the cache\n// or\napp.del('foo');\n// or an array of keys\napp.del(['foo', 'bar']);\n```\n\n### [.clear](index.js#L301)\n\nReset the entire cache to an empty object. Note that this does not also clear the `defaults` object, since you can manually do `cache.defaults = {}` if you want to reset that object as well.\n\n**Example**\n\n```js\n// clear \"defaults\" whenever the cache is cleared\napp.on('clear', key =\u003e (app.defaults = {}));\napp.clear();\n```\n\n### [.visit](index.js#L318)\n\nVisit (or map visit) the specified method (`key`) over the properties in the\ngiven object or array.\n\n**Params**\n\n* `key` **{String|Array}**: The name of the method to visit.\n* `val` **{Object|Array}**: The object or array to iterate over.\n* `returns` **{Object}**: Returns the instance for chaining.\n\n### [.keys](index.js#L338)\n\nGets an array of names of all enumerable properties on the cache.\n\n**Example**\n\n```js\nconst app = new CacheBase();\napp.set('user', true);\napp.set('admin', false);\n\nconsole.log(app.keys);\n//=\u003e ['user', 'admin']\n```\n\n### [.size](index.js#L357)\n\nGets the length of [keys](#keys).\n\n**Example**\n\n```js\nconst app = new CacheBase();\napp.set('user', true);\napp.set('admin', false);\n\nconsole.log(app.size);\n//=\u003e 2\n```\n\n## Usage examples\n\n**Create an instance of cache-base**\n\n```js\nconst app = new CacheBase();\n\napp.set('a', 'b');\napp.set('c.d', 'e');\n\nconsole.log(app.get('a'));\n//=\u003e 'b'\nconsole.log(app.get('c'));\n//=\u003e { d: 'e' }\nconsole.log(app);\n//=\u003e CacheBase { a: 'b' }\n```\n\n**Initialize with an object**\n\n```js\nconst app = new CacheBase({ a: 'b', c: { d: 'e' } });\n\nconsole.log(app.get('a'));\n//=\u003e 'b'\nconsole.log(app.get('c'));\n//=\u003e { d: 'e' }\nconsole.log(app.get('c.d'));\n//=\u003e 'e'\nconsole.log(app);\n//=\u003e CacheBase { cache: { a: 'b' } }\n```\n\n**Inherit**\n\n```js\nclass MyApp extends CacheBase {}\n\nconst app = new MyApp();\napp.set('a', 'b');\napp.set('c', 'd');\n\nconsole.log(app.get('a'));\n//=\u003e 'b'\n\nconsole.log(app);\n//=\u003e MyApp { cache: { a: 'b', c: 'd' } }\n```\n\n**Custom namespace**\n\nPass a string as the first value to the contructor to define a custom property name to use for the cache. By default values are stored on the `cache` property.\n\n```js\nconst CacheBase = require('cache-base');\nconst app = new CacheBase('data', { a: 'b' });\napp.set('c.d', 'e');\n\n// get values\nconsole.log(app.get('a'));\n//=\u003e 'b'\nconsole.log(app.get('c'));\n//=\u003e { d: 'e' }\nconsole.log(app.data);\n//=\u003e { a: 'b', c: { d: 'e' } }\nconsole.log(app);\n//=\u003e CacheBase { data: { a: 'b', c: { d: 'e' } } }\n```\n\n## About\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eContributing\u003c/strong\u003e\u003c/summary\u003e\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eRunning Tests\u003c/strong\u003e\u003c/summary\u003e\n\nRunning and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eBuilding docs\u003c/strong\u003e\u003c/summary\u003e\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n\u003c/details\u003e\n\n### Related projects\n\nYou might also be interested in these projects:\n\n* [base-methods](https://www.npmjs.com/package/base-methods): base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://github.com/jonschlinkert/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods \"base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.\")\n* [get-value](https://www.npmjs.com/package/get-value): Use property paths like 'a.b.c' to get a nested value from an object. Even works… [more](https://github.com/jonschlinkert/get-value) | [homepage](https://github.com/jonschlinkert/get-value \"Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other dot-prop library can do this!).\")\n* [has-value](https://www.npmjs.com/package/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://github.com/jonschlinkert/has-value) | [homepage](https://github.com/jonschlinkert/has-value \"Returns true if a value exists, false if empty. Works with deeply nested values using object paths.\")\n* [option-cache](https://www.npmjs.com/package/option-cache): Simple API for managing options in JavaScript applications. | [homepage](https://github.com/jonschlinkert/option-cache \"Simple API for managing options in JavaScript applications.\")\n* [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value \"Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.\")\n* [unset-value](https://www.npmjs.com/package/unset-value): Delete nested properties from an object using dot notation. | [homepage](https://github.com/jonschlinkert/unset-value \"Delete nested properties from an object using dot notation.\")\n\n### Contributors\n\n| **Commits** | **Contributor** | \n| --- | --- |\n| 67 | [jonschlinkert](https://github.com/jonschlinkert) |\n| 2 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |\n\n### Author\n\n**Jon Schlinkert**\n\n* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)\n* [GitHub Profile](https://github.com/jonschlinkert)\n* [Twitter Profile](https://twitter.com/jonschlinkert)\n\n### License\n\nCopyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 23, 2018._","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fcache-base","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonschlinkert%2Fcache-base","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fcache-base/lists"}