datamap-interface
Advanced tools
Comparing version 0.1.0 to 0.2.0
231
index.js
'use strict'; | ||
var datamapInterfacePrototype, isOwnProperty; | ||
/** | ||
* Cache. | ||
*/ | ||
var isOwnProperty; | ||
isOwnProperty = Object.prototype.hasOwnProperty; | ||
/** | ||
* An interface for a map of items. | ||
* | ||
* @constructor | ||
* @param {Object.<string, *>} values | ||
*/ | ||
function DatamapInterface(values) { | ||
this.map = {}; | ||
this.add(values); | ||
} | ||
datamapInterfacePrototype = DatamapInterface.prototype; | ||
/** | ||
* Detect if a key is defined on an object. | ||
* | ||
* @param {Object.<string, *>} object | ||
* @param {string} key | ||
* @return {boolean} | ||
*/ | ||
function has(object, property) { | ||
return isOwnProperty.call(object, property) && | ||
object[property] !== undefined; | ||
function has(object, key) { | ||
return isOwnProperty.call(object, key) && object[key] !== undefined; | ||
} | ||
/** | ||
* Loop over an `Object`. | ||
* | ||
* @param {Object.<string, *>} object | ||
* @param {function(string, *)} callback | ||
*/ | ||
function forPropertyInObject(object, callback) { | ||
var property; | ||
var key; | ||
for (property in object) { | ||
if (has(object, property)) { | ||
callback(property, object[property]); | ||
for (key in object) { | ||
if (has(object, key)) { | ||
callback(object[key], key); | ||
} | ||
@@ -29,63 +53,182 @@ } | ||
/** | ||
* Loop over an `Array`. | ||
* | ||
* @param {Array.<*>} array | ||
* @param {function(*, number)} callback | ||
*/ | ||
function forValueInArray(array, callback) { | ||
var iterator = -1, | ||
length = array.length; | ||
var index, | ||
length; | ||
while (++iterator < length) { | ||
callback(array[iterator], iterator); | ||
index = -1; | ||
length = array.length; | ||
while (++index < length) { | ||
callback(array[index], index); | ||
} | ||
} | ||
function add(object, property, value) { | ||
object[property] = value; | ||
} | ||
/** | ||
* Add all `values` to `object`. | ||
* | ||
* @param {Object.<string, *>} object | ||
* @param {Object.<string, *>} values | ||
*/ | ||
function addAll(object, values) { | ||
forPropertyInObject(values, function (property, value) { | ||
add(object, property, value); | ||
forPropertyInObject(values, function (value, key) { | ||
object[key] = value; | ||
}); | ||
} | ||
function remove(object, property) { | ||
object[property] = undefined; | ||
} | ||
/** | ||
* Remove every key in `keys` from `object`. | ||
* | ||
* @param {Object.<string, *>} object | ||
* @param {Array.<string>} keys | ||
*/ | ||
function removeAll(object, array) { | ||
forValueInArray(array, function (property) { | ||
remove(object, property); | ||
function removeAll(object, keys) { | ||
forValueInArray(keys, function (key) { | ||
object[key] = undefined; | ||
}); | ||
} | ||
function getProperty(object, property) { | ||
return has(object, property) ? object[property] : null; | ||
} | ||
/** | ||
* Add values to map. | ||
* | ||
* When the second argument is passed, it is treated as | ||
* a single value and the first parameter as a key. | ||
* Otherwise, every value in the first argument is added. | ||
* | ||
* @this DatamapInterface | ||
* @param {Object.<string, *>|string} values | ||
* @param {*} value | ||
*/ | ||
datamapInterfacePrototype.add = function (values, value) { | ||
function add(values, value) { | ||
var self; | ||
self = this; | ||
if (value) { | ||
add(this.map, values, value); | ||
self.map[values] = value; | ||
} else { | ||
addAll(this.map, values); | ||
addAll(self.map, values); | ||
} | ||
}; | ||
datamapInterfacePrototype.remove = function (values) { | ||
(typeof values === 'string' ? remove : removeAll)(this.map, values); | ||
}; | ||
return self; | ||
} | ||
datamapInterfacePrototype.all = function () { | ||
var values = {}; | ||
/** | ||
* Remove keys from map. | ||
* | ||
* When the second argument is passed, it is treated as | ||
* a single value and the first parameter as a key. | ||
* Otherwise, every value in the first argument is added. | ||
* | ||
* @this DatamapInterface | ||
* @param {Array.<string>|string} keys | ||
*/ | ||
function remove(values) { | ||
var self; | ||
self = this; | ||
if (typeof values === 'string') { | ||
self.map[values] = undefined; | ||
} else { | ||
removeAll(self.map, values); | ||
} | ||
return self; | ||
} | ||
/** | ||
* Get all values. | ||
* | ||
* @this DatamapInterface | ||
* @return {Object.<string, *>} | ||
*/ | ||
function all() { | ||
var values; | ||
values = {}; | ||
addAll(values, this.map); | ||
return values; | ||
}; | ||
} | ||
datamapInterfacePrototype.get = function (property) { | ||
return getProperty(this.map, property); | ||
}; | ||
/** | ||
* Get all keys. | ||
* | ||
* @this DatamapInterface | ||
* @return {Array.<string>} | ||
*/ | ||
datamapInterfacePrototype.has = function (property) { | ||
return has(this.map, property); | ||
}; | ||
function getKeys() { | ||
var result, | ||
index; | ||
exports = module.exports = DatamapInterface; | ||
result = []; | ||
index = -1; | ||
forPropertyInObject(this.map, function (value, key) { | ||
result[++index] = key; | ||
}); | ||
return result; | ||
} | ||
/** | ||
* Get a value. | ||
* | ||
* @this DatamapInterface | ||
* @param {string} key | ||
* @return {*} | ||
*/ | ||
function get(key) { | ||
return has(this.map, key) ? this.map[key] : null; | ||
} | ||
/** | ||
* Whether or not `value` is in context. | ||
* | ||
* @this DatamapInterface | ||
* @param {*} value | ||
* @return {boolean} | ||
*/ | ||
function datamapHas(key) { | ||
return has(this.map, key); | ||
} | ||
/** | ||
* Expose methods on prototype. | ||
*/ | ||
var datamapInterfacePrototype; | ||
datamapInterfacePrototype = DatamapInterface.prototype; | ||
datamapInterfacePrototype.add = add; | ||
datamapInterfacePrototype.remove = remove; | ||
datamapInterfacePrototype.all = all; | ||
datamapInterfacePrototype.valueOf = all; | ||
datamapInterfacePrototype.toJSON = all; | ||
datamapInterfacePrototype.get = get; | ||
datamapInterfacePrototype.has = datamapHas; | ||
datamapInterfacePrototype.keys = getKeys; | ||
/** | ||
* Expose `DatamapInterface`. | ||
*/ | ||
module.exports = DatamapInterface; |
{ | ||
"name": "datamap-interface", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Simple interface for a map functioning as a database", | ||
@@ -11,2 +11,6 @@ "license": "MIT", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/datamap-interface.git" | ||
}, | ||
"author": "Titus Wormer <tituswormer@gmail.com>", | ||
@@ -16,37 +20,15 @@ "devDependencies": { | ||
"istanbul": "^0.3.0", | ||
"jscs": "^1.5.0", | ||
"jscs": "^1.7.0", | ||
"mocha": "^1.20.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/datamap-interface.git" | ||
}, | ||
"scripts": { | ||
"test": "node_modules/.bin/_mocha --reporter spec --check-leaks -u exports spec/datamap-interface.spec.js", | ||
"test-travis": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- --reporter spec --check-leaks -u exports spec/datamap-interface.spec.js", | ||
"test": "node_modules/.bin/_mocha --reporter spec --check-leaks -u exports test.js", | ||
"test-travis": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- --reporter spec --check-leaks -u exports test.js", | ||
"lint": "npm run lint-api && npm run lint-test && npm run lint-style", | ||
"lint-api": "node_modules/.bin/eslint index.js --env node --env browser --rule 'quotes: [2, single]'", | ||
"lint-test": "node_modules/.bin/eslint spec/datamap-interface.spec.js --env node --env mocha --rule 'quotes: [2, single]'", | ||
"lint-style": "node_modules/.bin/jscs index.js spec/datamap-interface.spec.js --reporter=inline", | ||
"install-browser-test": "npm install browserify", | ||
"build-browser-test": "node_modules/.bin/browserify spec/datamap-interface.spec.js -o spec/browser.spec.js", | ||
"coverage": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -- spec/datamap-interface.spec.js", | ||
"lint-test": "node_modules/.bin/eslint test.js --env node --env mocha --rule 'quotes: [2, single]'", | ||
"lint-style": "node_modules/.bin/jscs index.js test.js --reporter=inline", | ||
"coverage": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -- test.js", | ||
"make": "npm run lint && npm run coverage" | ||
}, | ||
"testling": { | ||
"files": "spec/datamap-interface.spec.js", | ||
"harness": "mocha", | ||
"browsers": [ | ||
"iexplore/latest", | ||
"chrome/latest", | ||
"chrome/canary", | ||
"firefox/latest", | ||
"firefox/nightly", | ||
"opera/latest", | ||
"opera/next", | ||
"safari/latest", | ||
"iphone/latest", | ||
"android-browser/latest" | ||
] | ||
} | ||
} |
# datamap-interface [![Build Status](https://travis-ci.org/wooorm/datamap-interface.svg?branch=master)](https://travis-ci.org/wooorm/datamap-interface) [![Coverage Status](https://img.shields.io/coveralls/wooorm/datamap-interface.svg)](https://coveralls.io/r/wooorm/datamap-interface?branch=master) | ||
A simple interface for a map functioning as a database. | ||
A simple interface for a map. | ||
@@ -25,6 +25,5 @@ ## Installation | ||
```js | ||
var DatamapInterface = require('datamap-interface'), | ||
animals; | ||
var DatamapInterface = require('datamap-interface'); | ||
animals = new DatamapInterface({ | ||
var animals = new DatamapInterface({ | ||
'shark' : 'fish', | ||
@@ -39,7 +38,5 @@ 'tuna' : 'fish', | ||
animals.add('unicorn', 'mammal'); | ||
animals.get('unicorn'); // 'mammal' | ||
animals.add('unicorn', 'mammal').get('unicorn'); // 'mammal' | ||
animals.remove('unicorn'); | ||
animals.has('unicorn'); // false | ||
animals.remove('unicorn').has('unicorn'); // false | ||
``` | ||
@@ -54,6 +51,5 @@ | ||
```js | ||
var DatamapInterface = require('datamap-interface'), | ||
fish; | ||
var DatamapInterface = require('datamap-interface'); | ||
animals = new DatamapInterface({ | ||
var animals = new DatamapInterface({ | ||
'unicorn' : 'mystical creature', | ||
@@ -67,4 +63,2 @@ 'shark' : 'fish', | ||
The following functions are available on the instance: | ||
### DatamapInterface#has(key) | ||
@@ -86,5 +80,5 @@ | ||
Gets the value for `key` in map, or `null`. | ||
Get the value for `key` in map, or `null`. | ||
### DatamapInterface#add(key, value) | ||
### DatamapInterface#add() | ||
@@ -99,4 +93,7 @@ ```js | ||
Either adds the key/value pair to the map, or every key/value pair in the first argument. | ||
- `DatamapInterface#add(key, value)`: Add `value` as `key` to map; | ||
- `DatamapInterface#add(values)`: Add every item in `values` to map. | ||
Returns self. | ||
### DatamapInterface#remove(keys) | ||
@@ -109,11 +106,25 @@ | ||
Removes `keys` or every key in `keys`. | ||
- `DatamapInterface#remove(key)`: Remove `key` from map; | ||
- `DatamapInterface#remove(keys)`: Remove every key in `keys` from map. | ||
Given values are **NOT** validated; no error is thrown when non-existent values are removed. | ||
Returns self. No error is thrown when non-existent values are removed. | ||
### DatamapInterface#keys() | ||
```js | ||
animals.keys(); // ['shark', 'tuna', 'colugo', 'unicorn'] | ||
``` | ||
Return the map as an `Object`. | ||
### DatamapInterface#all() | ||
> Alias: `DatamapInterface#valueOf()` | ||
> Alias: `DatamapInterface#toJSON()` | ||
```js | ||
animals.all(); | ||
/* { | ||
/** | ||
* { | ||
* 'shark' : 'fish', | ||
@@ -127,3 +138,3 @@ * 'tuna' : 'fish', | ||
Return the values (as an Object) in the internal database. | ||
Return the map as an `Object`. | ||
@@ -130,0 +141,0 @@ ## License |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
136
8957
4
183
1