├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | end_of_line = lf 7 | charset = utf-8 8 | indent_size = 2 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | 16 | [**/{actual,fixtures,expected}/**] 17 | trim_trailing_whitespace = false 18 | insert_final_newline = false 19 | 20 | [**/templates/**] 21 | trim_trailing_whitespace = false 22 | insert_final_newline = false 23 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | "globals": { 13 | "document": false, 14 | "navigator": false, 15 | "window": false 16 | }, 17 | "rules": { 18 | "accessor-pairs": 2, 19 | "arrow-spacing": [ 20 | 2, 21 | { 22 | "before": true, 23 | "after": true 24 | } 25 | ], 26 | "block-spacing": [ 27 | 2, 28 | "always" 29 | ], 30 | "brace-style": [ 31 | 2, 32 | "1tbs", 33 | { 34 | "allowSingleLine": true 35 | } 36 | ], 37 | "comma-dangle": [ 38 | 2, 39 | "never" 40 | ], 41 | "comma-spacing": [ 42 | 2, 43 | { 44 | "before": false, 45 | "after": true 46 | } 47 | ], 48 | "comma-style": [ 49 | 2, 50 | "last" 51 | ], 52 | "constructor-super": 2, 53 | "curly": [ 54 | 2, 55 | "multi-line" 56 | ], 57 | "dot-location": [ 58 | 2, 59 | "property" 60 | ], 61 | "eol-last": 2, 62 | "eqeqeq": [ 63 | 2, 64 | "allow-null" 65 | ], 66 | "generator-star-spacing": [ 67 | 2, 68 | { 69 | "before": true, 70 | "after": true 71 | } 72 | ], 73 | "handle-callback-err": [ 74 | 2, 75 | "^(err|error)$" 76 | ], 77 | "indent": [ 78 | 2, 79 | 2, 80 | { 81 | "SwitchCase": 1 82 | } 83 | ], 84 | "key-spacing": [ 85 | 2, 86 | { 87 | "beforeColon": false, 88 | "afterColon": true 89 | } 90 | ], 91 | "keyword-spacing": [ 92 | 2, 93 | { 94 | "before": true, 95 | "after": true 96 | } 97 | ], 98 | "new-cap": [ 99 | 2, 100 | { 101 | "newIsCap": true, 102 | "capIsNew": false 103 | } 104 | ], 105 | "new-parens": 2, 106 | "no-array-constructor": 2, 107 | "no-caller": 2, 108 | "no-class-assign": 2, 109 | "no-cond-assign": 2, 110 | "no-const-assign": 2, 111 | "no-control-regex": 2, 112 | "no-debugger": 2, 113 | "no-delete-var": 2, 114 | "no-dupe-args": 2, 115 | "no-dupe-class-members": 2, 116 | "no-dupe-keys": 2, 117 | "no-duplicate-case": 2, 118 | "no-empty-character-class": 2, 119 | "no-eval": 2, 120 | "no-ex-assign": 2, 121 | "no-extend-native": 2, 122 | "no-extra-bind": 2, 123 | "no-extra-boolean-cast": 2, 124 | "no-extra-parens": [ 125 | 2, 126 | "functions" 127 | ], 128 | "no-fallthrough": 2, 129 | "no-floating-decimal": 2, 130 | "no-func-assign": 2, 131 | "no-implied-eval": 2, 132 | "no-inner-declarations": [ 133 | 2, 134 | "functions" 135 | ], 136 | "no-invalid-regexp": 2, 137 | "no-irregular-whitespace": 2, 138 | "no-iterator": 2, 139 | "no-label-var": 2, 140 | "no-labels": 2, 141 | "no-lone-blocks": 2, 142 | "no-mixed-spaces-and-tabs": 2, 143 | "no-multi-spaces": 2, 144 | "no-multi-str": 2, 145 | "no-multiple-empty-lines": [ 146 | 2, 147 | { 148 | "max": 1 149 | } 150 | ], 151 | "no-native-reassign": 0, 152 | "no-negated-in-lhs": 2, 153 | "no-new": 2, 154 | "no-new-func": 2, 155 | "no-new-object": 2, 156 | "no-new-require": 2, 157 | "no-new-wrappers": 2, 158 | "no-obj-calls": 2, 159 | "no-octal": 2, 160 | "no-octal-escape": 2, 161 | "no-proto": 0, 162 | "no-redeclare": 2, 163 | "no-regex-spaces": 2, 164 | "no-return-assign": 2, 165 | "no-self-compare": 2, 166 | "no-sequences": 2, 167 | "no-shadow-restricted-names": 2, 168 | "no-spaced-func": 2, 169 | "no-sparse-arrays": 2, 170 | "no-this-before-super": 2, 171 | "no-throw-literal": 2, 172 | "no-trailing-spaces": 0, 173 | "no-undef": 2, 174 | "no-undef-init": 2, 175 | "no-unexpected-multiline": 2, 176 | "no-unneeded-ternary": [ 177 | 2, 178 | { 179 | "defaultAssignment": false 180 | } 181 | ], 182 | "no-unreachable": 2, 183 | "no-unused-vars": [ 184 | 2, 185 | { 186 | "vars": "all", 187 | "args": "none" 188 | } 189 | ], 190 | "no-useless-call": 0, 191 | "no-with": 2, 192 | "one-var": [ 193 | 0, 194 | { 195 | "initialized": "never" 196 | } 197 | ], 198 | "operator-linebreak": [ 199 | 0, 200 | "after", 201 | { 202 | "overrides": { 203 | "?": "before", 204 | ":": "before" 205 | } 206 | } 207 | ], 208 | "padded-blocks": [ 209 | 0, 210 | "never" 211 | ], 212 | "quotes": [ 213 | 2, 214 | "single", 215 | "avoid-escape" 216 | ], 217 | "radix": 2, 218 | "semi": [ 219 | 2, 220 | "always" 221 | ], 222 | "semi-spacing": [ 223 | 2, 224 | { 225 | "before": false, 226 | "after": true 227 | } 228 | ], 229 | "space-before-blocks": [ 230 | 2, 231 | "always" 232 | ], 233 | "space-before-function-paren": [ 234 | 2, 235 | "never" 236 | ], 237 | "space-in-parens": [ 238 | 2, 239 | "never" 240 | ], 241 | "space-infix-ops": 2, 242 | "space-unary-ops": [ 243 | 2, 244 | { 245 | "words": true, 246 | "nonwords": false 247 | } 248 | ], 249 | "spaced-comment": [ 250 | 0, 251 | "always", 252 | { 253 | "markers": [ 254 | "global", 255 | "globals", 256 | "eslint", 257 | "eslint-disable", 258 | "*package", 259 | "!", 260 | "," 261 | ] 262 | } 263 | ], 264 | "use-isnan": 2, 265 | "valid-typeof": 2, 266 | "wrap-iife": [ 267 | 2, 268 | "any" 269 | ], 270 | "yoda": [ 271 | 2, 272 | "never" 273 | ] 274 | } 275 | } 276 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | *.DS_store 3 | *.sublime-* 4 | _gh_pages 5 | bower_components 6 | node_modules 7 | npm-debug.log 8 | actual 9 | test/actual 10 | temp 11 | tmp 12 | TODO.md 13 | vendor -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - stable 5 | - '5' 6 | - '4' 7 | - '0.12' 8 | - '0.10' 9 | matrix: 10 | fast_finish: true 11 | allow_failures: 12 | - node_js: '0.10' 13 | git: 14 | depth: 10 15 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | Based on MapCache in Lo-dash v3.0. [MIT License](https://github.com/lodash/lodash/blob/master/LICENSE.txt) 2 | 3 | ## Usage 4 | 5 | ```js 6 | var MapCache = require('{%= name %}'); 7 | var mapCache = new MapCache(); 8 | ``` 9 | 10 | ## API 11 | 12 | {%= apidocs("index.js") %} -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2016, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # map-cache [![NPM version](https://img.shields.io/npm/v/map-cache.svg?style=flat)](https://www.npmjs.com/package/map-cache) [![NPM downloads](https://img.shields.io/npm/dm/map-cache.svg?style=flat)](https://npmjs.org/package/map-cache) [![Build Status](https://img.shields.io/travis/jonschlinkert/map-cache.svg?style=flat)](https://travis-ci.org/jonschlinkert/map-cache) 2 | 3 | Basic cache object for storing key-value pairs. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install map-cache --save 11 | ``` 12 | 13 | Based on MapCache in Lo-dash v3.0. [MIT License](https://github.com/lodash/lodash/blob/master/LICENSE.txt) 14 | 15 | ## Usage 16 | 17 | ```js 18 | var MapCache = require('map-cache'); 19 | var mapCache = new MapCache(); 20 | ``` 21 | 22 | ## API 23 | 24 | ### [MapCache](index.js#L28) 25 | 26 | Creates a cache object to store key/value pairs. 27 | 28 | **Example** 29 | 30 | ```js 31 | var cache = new MapCache(); 32 | ``` 33 | 34 | ### [.set](index.js#L45) 35 | 36 | Adds `value` to `key` on the cache. 37 | 38 | **Params** 39 | 40 | * `key` **{String}**: The key of the value to cache. 41 | * `value` **{any}**: The value to cache. 42 | * `returns` **{Object}**: Returns the `Cache` object for chaining. 43 | 44 | **Example** 45 | 46 | ```js 47 | cache.set('foo', 'bar'); 48 | ``` 49 | 50 | ### [.get](index.js#L65) 51 | 52 | Gets the cached value for `key`. 53 | 54 | **Params** 55 | 56 | * `key` **{String}**: The key of the value to get. 57 | * `returns` **{any}**: Returns the cached value. 58 | 59 | **Example** 60 | 61 | ```js 62 | cache.get('foo'); 63 | //=> 'bar' 64 | ``` 65 | 66 | ### [.has](index.js#L82) 67 | 68 | Checks if a cached value for `key` exists. 69 | 70 | **Params** 71 | 72 | * `key` **{String}**: The key of the entry to check. 73 | * `returns` **{Boolean}**: Returns `true` if an entry for `key` exists, else `false`. 74 | 75 | **Example** 76 | 77 | ```js 78 | cache.has('foo'); 79 | //=> true 80 | ``` 81 | 82 | ### [.del](index.js#L98) 83 | 84 | Removes `key` and its value from the cache. 85 | 86 | **Params** 87 | 88 | * `key` **{String}**: The key of the value to remove. 89 | * `returns` **{Boolean}**: Returns `true` if the entry was removed successfully, else `false`. 90 | 91 | **Example** 92 | 93 | ```js 94 | cache.del('foo'); 95 | ``` 96 | 97 | ## Related projects 98 | 99 | You might also be interested in these projects: 100 | 101 | * [cache-base](https://www.npmjs.com/package/cache-base): Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects. | [homepage](https://github.com/jonschlinkert/cache-base) 102 | * [config-cache](https://www.npmjs.com/package/config-cache): General purpose JavaScript object storage methods. | [homepage](https://github.com/jonschlinkert/config-cache) 103 | * [option-cache](https://www.npmjs.com/package/option-cache): Simple API for managing options in JavaScript applications. | [homepage](https://github.com/jonschlinkert/option-cache) 104 | 105 | ## Contributing 106 | 107 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/map-cache/issues/new). 108 | 109 | ## Building docs 110 | 111 | Generate readme and API documentation with [verb](https://github.com/verbose/verb): 112 | 113 | ```sh 114 | $ npm install verb && npm run docs 115 | ``` 116 | 117 | Or, if [verb](https://github.com/verbose/verb) is installed globally: 118 | 119 | ```sh 120 | $ verb 121 | ``` 122 | 123 | ## Running tests 124 | 125 | Install dev dependencies: 126 | 127 | ```sh 128 | $ npm install -d && npm test 129 | ``` 130 | 131 | ## Author 132 | 133 | **Jon Schlinkert** 134 | 135 | * [github/jonschlinkert](https://github.com/jonschlinkert) 136 | * [twitter/jonschlinkert](http://twitter.com/jonschlinkert) 137 | 138 | ## License 139 | 140 | Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). 141 | Released under the [MIT license](https://github.com/jonschlinkert/map-cache/blob/master/LICENSE). 142 | 143 | *** 144 | 145 | _This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on May 10, 2016._ -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * map-cache 3 | * 4 | * Copyright (c) 2015, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var hasOwn = Object.prototype.hasOwnProperty; 11 | 12 | /** 13 | * Expose `MapCache` 14 | */ 15 | 16 | module.exports = MapCache; 17 | 18 | /** 19 | * Creates a cache object to store key/value pairs. 20 | * 21 | * ```js 22 | * var cache = new MapCache(); 23 | * ``` 24 | * 25 | * @api public 26 | */ 27 | 28 | function MapCache(data) { 29 | this.__data__ = data || {}; 30 | } 31 | 32 | /** 33 | * Adds `value` to `key` on the cache. 34 | * 35 | * ```js 36 | * cache.set('foo', 'bar'); 37 | * ``` 38 | * 39 | * @param {String} `key` The key of the value to cache. 40 | * @param {*} `value` The value to cache. 41 | * @returns {Object} Returns the `Cache` object for chaining. 42 | * @api public 43 | */ 44 | 45 | MapCache.prototype.set = function mapSet(key, value) { 46 | if (key !== '__proto__') { 47 | this.__data__[key] = value; 48 | } 49 | return this; 50 | }; 51 | 52 | /** 53 | * Gets the cached value for `key`. 54 | * 55 | * ```js 56 | * cache.get('foo'); 57 | * //=> 'bar' 58 | * ``` 59 | * 60 | * @param {String} `key` The key of the value to get. 61 | * @returns {*} Returns the cached value. 62 | * @api public 63 | */ 64 | 65 | MapCache.prototype.get = function mapGet(key) { 66 | return key === '__proto__' ? undefined : this.__data__[key]; 67 | }; 68 | 69 | /** 70 | * Checks if a cached value for `key` exists. 71 | * 72 | * ```js 73 | * cache.has('foo'); 74 | * //=> true 75 | * ``` 76 | * 77 | * @param {String} `key` The key of the entry to check. 78 | * @returns {Boolean} Returns `true` if an entry for `key` exists, else `false`. 79 | * @api public 80 | */ 81 | 82 | MapCache.prototype.has = function mapHas(key) { 83 | return key !== '__proto__' && hasOwn.call(this.__data__, key); 84 | }; 85 | 86 | /** 87 | * Removes `key` and its value from the cache. 88 | * 89 | * ```js 90 | * cache.del('foo'); 91 | * ``` 92 | * @title .del 93 | * @param {String} `key` The key of the value to remove. 94 | * @returns {Boolean} Returns `true` if the entry was removed successfully, else `false`. 95 | * @api public 96 | */ 97 | 98 | MapCache.prototype.del = function mapDelete(key) { 99 | return this.has(key) && delete this.__data__[key]; 100 | }; 101 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "map-cache", 3 | "description": "Basic cache object for storing key-value pairs.", 4 | "version": "0.2.2", 5 | "homepage": "https://github.com/jonschlinkert/map-cache", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/map-cache", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/map-cache/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=0.10.0" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "devDependencies": { 23 | "gulp-format-md": "^0.1.9", 24 | "should": "^8.3.1" 25 | }, 26 | "keywords": [ 27 | "cache", 28 | "get", 29 | "has", 30 | "object", 31 | "set", 32 | "storage", 33 | "store" 34 | ], 35 | "verb": { 36 | "run": true, 37 | "toc": false, 38 | "layout": "default", 39 | "tasks": [ 40 | "readme" 41 | ], 42 | "plugins": [ 43 | "gulp-format-md" 44 | ], 45 | "related": { 46 | "list": [ 47 | "config-cache", 48 | "option-cache", 49 | "cache-base" 50 | ] 51 | }, 52 | "reflinks": [ 53 | "verb" 54 | ], 55 | "lint": { 56 | "reflinks": true 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * map-cache 3 | * 4 | * Copyright (c) 2015, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var assert = require('assert'); 11 | var should = require('should'); 12 | var MapCache = require('./'); 13 | 14 | describe('mapCache', function() { 15 | var cache = new MapCache; 16 | 17 | it('should set values on the `__data__` object on the cache:', function() { 18 | cache.set('a', 'b'); 19 | cache.should.eql({ __data__: { a: 'b' }}); 20 | cache.del('a'); 21 | }); 22 | 23 | it('should delete values from the `__data__` object:', function() { 24 | cache.set('a', 'b'); 25 | cache.del('a'); 26 | cache.should.eql({ __data__: {}}); 27 | }); 28 | 29 | it('should get values from the `__data__` object:', function() { 30 | cache.set('a', 'b'); 31 | cache.get('a').should.equal('b'); 32 | cache.get('a').should.not.equal('a'); 33 | }); 34 | 35 | it('should return `true` if `__data__` has `key`:', function() { 36 | cache.set('a', 'b'); 37 | cache.has('a').should.be.true; 38 | cache.has('a').should.not.be.false; 39 | }); 40 | }); 41 | --------------------------------------------------------------------------------