underscore.db
Advanced tools
Comparing version 0.4.0 to 0.5.0
{ | ||
"name": "underscore.db", | ||
"main": "dist/underscore.db.js", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"homepage": "https://github.com/typicode/underscore.db", | ||
@@ -6,0 +6,0 @@ "authors": [ |
@@ -1,119 +0,129 @@ | ||
(function(root) { | ||
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var index = require('./'); | ||
// Set an empty reference to _ | ||
var _; | ||
index.save = function(db, destination) { | ||
destination = destination || 'db'; | ||
localStorage.setItem(destination, JSON.stringify(db, null, 2)); | ||
}; | ||
// UUID | ||
// https://gist.github.com/jed/982883 | ||
/* jshint ignore:start */ | ||
function b( | ||
a // placeholder | ||
){ | ||
return a // if the placeholder was passed, return | ||
? ( // a random number from 0 to 15 | ||
a ^ // unless b is 8, | ||
Math.random() // in which case | ||
* 16 // a random number from | ||
>> a/4 // 8 to 11 | ||
).toString(16) // in hexadecimal | ||
: ( // or otherwise a concatenated string: | ||
[1e7] + // 10000000 + | ||
-1e3 + // -1000 + | ||
-4e3 + // -4000 + | ||
-8e3 + // -80000000 + | ||
-1e11 // -100000000000, | ||
).replace( // replacing | ||
/[018]/g, // zeroes, ones, and eights with | ||
b // random hex digits | ||
) | ||
} | ||
/* jshint ignore:end */ | ||
index.load = function(source) { | ||
source = source || 'db'; | ||
return JSON.parse(localStorage.getItem(source)); | ||
}; | ||
// Copies properties from an object to another | ||
function __update(dest, src) { | ||
_.each(src, function(value, key) { | ||
dest[key] = value; | ||
}); | ||
} | ||
_.mixin(index); | ||
},{"./":2}],2:[function(require,module,exports){ | ||
// Set an empty reference to _ | ||
var _; | ||
// Removes an item from an array | ||
function __remove(array, item) { | ||
var index = _.indexOf(array, item); | ||
if (index != -1) array.splice(index, 1); | ||
} | ||
// UUID | ||
// https://gist.github.com/jed/982883 | ||
/* jshint ignore:start */ | ||
function b( | ||
a // placeholder | ||
){ | ||
return a // if the placeholder was passed, return | ||
? ( // a random number from 0 to 15 | ||
a ^ // unless b is 8, | ||
Math.random() // in which case | ||
* 16 // a random number from | ||
>> a/4 // 8 to 11 | ||
).toString(16) // in hexadecimal | ||
: ( // or otherwise a concatenated string: | ||
[1e7] + // 10000000 + | ||
-1e3 + // -1000 + | ||
-4e3 + // -4000 + | ||
-8e3 + // -80000000 + | ||
-1e11 // -100000000000, | ||
).replace( // replacing | ||
/[018]/g, // zeroes, ones, and eights with | ||
b // random hex digits | ||
) | ||
} | ||
/* jshint ignore:end */ | ||
function get(collection, id) { | ||
return _.find(collection, function (doc) { | ||
return doc.id === id; | ||
}); | ||
} | ||
// Copies properties from an docect to another | ||
function __update(dest, src) { | ||
_.each(src, function(value, key) { | ||
dest[key] = value; | ||
}); | ||
} | ||
function createId(collection) { | ||
return b(); | ||
} | ||
// Removes an item from an array | ||
function __remove(array, item) { | ||
var index = _.indexOf(array, item); | ||
if (index != -1) array.splice(index, 1); | ||
} | ||
function insert(collection, obj) { | ||
var clone = _.clone(obj); | ||
function get(collection, id) { | ||
return _.find(collection, function (doc) { | ||
return doc.id === id; | ||
}); | ||
} | ||
if (!clone.hasOwnProperty('id')) clone.id = _.createId(collection); | ||
function createId(collection, doc) { | ||
return b(); | ||
} | ||
collection.push(clone); | ||
function insert(collection, doc) { | ||
var clone = _.clone(doc); | ||
return clone; | ||
} | ||
if (!clone.hasOwnProperty('id')) clone.id = _.createId(collection, doc); | ||
function update(collection, id, attrs) { | ||
var doc = get(collection, id); | ||
collection.push(clone); | ||
if (doc) __update(doc, attrs); | ||
return clone; | ||
} | ||
return doc; | ||
} | ||
function update(collection, id, attrs) { | ||
var doc = get(collection, id); | ||
function updateWhere(collection, whereAttrs, attrs) { | ||
var docs = _.where(collection, whereAttrs); | ||
if (doc) __update(doc, attrs); | ||
docs.forEach(function(doc) { | ||
__update(doc, attrs); | ||
}); | ||
return doc; | ||
} | ||
return docs; | ||
} | ||
function updateWhere(collection, whereAttrs, attrs) { | ||
var docs = _.where(collection, whereAttrs); | ||
function remove(collection, id) { | ||
var doc = get(collection, id); | ||
docs.forEach(function(doc) { | ||
__update(doc, attrs); | ||
}); | ||
__remove(collection, doc); | ||
return docs; | ||
} | ||
return doc; | ||
} | ||
function remove(collection, id) { | ||
var doc = get(collection, id); | ||
function removeWhere(collection, attrs) { | ||
var docs = _.where(collection, attrs); | ||
__remove(collection, doc); | ||
docs.forEach(function(doc) { | ||
__remove(collection, doc); | ||
}); | ||
return doc; | ||
} | ||
return docs; | ||
} | ||
function removeWhere(collection, attrs) { | ||
var docs = _.where(collection, attrs); | ||
function mixWith(lib) { | ||
_ = lib; | ||
_.mixin({ | ||
get: get, | ||
createId: createId, | ||
insert: insert, | ||
update: update, | ||
updateWhere: updateWhere, | ||
remove: remove, | ||
removeWhere: removeWhere | ||
}); | ||
} | ||
docs.forEach(function(doc) { | ||
__remove(collection, doc); | ||
}); | ||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports.mixWith = mixWith; | ||
} else { | ||
mixWith(root._); | ||
} | ||
})(this); | ||
return docs; | ||
} | ||
function set_(lib) { | ||
_ = lib; | ||
} | ||
module.exports = { | ||
set_: set_, | ||
get: get, | ||
createId: createId, | ||
insert: insert, | ||
update: update, | ||
updateWhere: updateWhere, | ||
remove: remove, | ||
removeWhere: removeWhere | ||
}; | ||
},{}]},{},[1]) |
@@ -1,2 +0,2 @@ | ||
/*! underscore.db 0.4.0 */ | ||
!function(a){function b(a){return a?(a^16*Math.random()>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,b)}function c(a,b){m.each(b,function(b,c){a[c]=b})}function d(a,b){var c=m.indexOf(a,b);-1!=c&&a.splice(c,1)}function e(a,b){return m.find(a,function(a){return a.id===b})}function f(){return b()}function g(a,b){var c=m.clone(b);return c.hasOwnProperty("id")||(c.id=m.createId(a)),a.push(c),c}function h(a,b,d){var f=e(a,b);return f&&c(f,d),f}function i(a,b,d){var e=m.where(a,b);return e.forEach(function(a){c(a,d)}),e}function j(a,b){var c=e(a,b);return d(a,c),c}function k(a,b){var c=m.where(a,b);return c.forEach(function(b){d(a,b)}),c}function l(a){m=a,m.mixin({get:e,createId:f,insert:g,update:h,updateWhere:i,remove:j,removeWhere:k})}var m;"undefined"!=typeof module&&module.exports?module.exports.mixWith=l:l(a._)}(this); | ||
/*! underscore.db 0.5.0 */ | ||
!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a){var b=a("./");b.save=function(a,b){b=b||"db",localStorage.setItem(b,JSON.stringify(a,null,2))},b.load=function(a){return a=a||"db",JSON.parse(localStorage.getItem(a))},_.mixin(b)},{"./":2}],2:[function(a,b){function c(a){return a?(a^16*Math.random()>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,c)}function d(a,b){n.each(b,function(b,c){a[c]=b})}function e(a,b){var c=n.indexOf(a,b);-1!=c&&a.splice(c,1)}function f(a,b){return n.find(a,function(a){return a.id===b})}function g(){return c()}function h(a,b){var c=n.clone(b);return c.hasOwnProperty("id")||(c.id=n.createId(a,b)),a.push(c),c}function i(a,b,c){var e=f(a,b);return e&&d(e,c),e}function j(a,b,c){var e=n.where(a,b);return e.forEach(function(a){d(a,c)}),e}function k(a,b){var c=f(a,b);return e(a,c),c}function l(a,b){var c=n.where(a,b);return c.forEach(function(b){e(a,b)}),c}function m(a){n=a}var n;b.exports={set_:m,get:f,createId:g,insert:h,update:i,updateWhere:j,remove:k,removeWhere:l}},{}]},{},[1]); |
@@ -5,7 +5,8 @@ module.exports = function(grunt) { | ||
pkg: grunt.file.readJSON('package.json'), | ||
copy: { | ||
main: { | ||
src: 'src/index.js', | ||
dest: 'dist/<%= pkg.name %>.js', | ||
}, | ||
browserify: { | ||
dist: { | ||
files: { | ||
'dist/<%= pkg.name %>.js': 'src/browser.js' | ||
} | ||
} | ||
}, | ||
@@ -41,11 +42,11 @@ uglify: { | ||
grunt.loadNpmTasks('grunt-contrib-uglify'); | ||
grunt.loadNpmTasks('grunt-browserify'); | ||
grunt.loadNpmTasks('grunt-contrib-jshint'); | ||
grunt.loadNpmTasks('grunt-mocha-test'); | ||
grunt.loadNpmTasks('grunt-contrib-watch'); | ||
grunt.loadNpmTasks('grunt-contrib-copy'); | ||
grunt.registerTask('test', ['jshint', 'mochaTest']); | ||
grunt.registerTask('default', ['jshint', 'mochaTest', 'copy', 'uglify']); | ||
grunt.registerTask('default', ['jshint', 'mochaTest', 'browserify', 'uglify']); | ||
}; |
{ | ||
"name": "underscore.db", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Use JavaScript objects as databases", | ||
"main": "dist/underscore.db.js", | ||
"main": "src/node.js", | ||
"scripts": { | ||
@@ -33,5 +33,5 @@ "test": "grunt test" | ||
"grunt-contrib-watch": "~0.5.3", | ||
"grunt-contrib-copy": "~0.5.0", | ||
"grunt-cli": "~0.1.11" | ||
"grunt-cli": "~0.1.11", | ||
"grunt-browserify": "~1.3.0" | ||
} | ||
} |
@@ -17,5 +17,5 @@ [![Build Status](https://travis-ci.org/typicode/underscore.db.png)](https://travis-ci.org/typicode/underscore.db) | ||
It adds ```get```, ```insert```, ```update```, ```updateWhere```, ```remove```, ```removeWhere``` and ```createId```. | ||
It adds `get`, `insert`, `update`, `updateWhere`, `remove`, `removeWhere`, `save`, `load and `createId`. | ||
This results in a very small mixin that is less than 260 bytes (minified and gzipped) and that can be embedded anywhere. | ||
This results in a very small mixin that is less than 0.4kb (minified and gzipped) and that can be embedded anywhere. | ||
@@ -108,2 +108,18 @@ ## When to use | ||
### save(db, [destination]) | ||
Persists database using localStorage or filesystem. If no destination is specified it will save to `db` or `./db.json`. | ||
```javascript | ||
_.save(db); | ||
``` | ||
### load([source]) | ||
Loads database from localStorage or filesystem. If no source is specified it will load from `db` or `./db.json`. | ||
```javascript | ||
var db = _.load(); | ||
``` | ||
## Install | ||
@@ -164,19 +180,2 @@ | ||
### How to persist? | ||
Persisting and loading an object in JavaScript is very easy and therefore saving a database-like object too. | ||
Example: | ||
```javascript | ||
// Node | ||
var fs = require('fs'); | ||
fs.writeFileSync('db.json', JSON.stringify(db)); | ||
var db = require('./db.json'); | ||
// Browser | ||
localStorage.setItem('db', JSON.stringify(db)); | ||
var db = JSON.parse(localStorage.getItem('db')); | ||
``` | ||
### How to create a custom build? | ||
@@ -209,5 +208,4 @@ | ||
_.createId = function(collection) { | ||
// ... | ||
return id; | ||
_.createId = function(collection, doc) { | ||
return collection + Date.now(); | ||
} | ||
@@ -214,0 +212,0 @@ ``` |
185
src/index.js
@@ -1,119 +0,112 @@ | ||
(function(root) { | ||
// Set an empty reference to _ | ||
var _; | ||
// Set an empty reference to _ | ||
var _; | ||
// UUID | ||
// https://gist.github.com/jed/982883 | ||
/* jshint ignore:start */ | ||
function b( | ||
a // placeholder | ||
){ | ||
return a // if the placeholder was passed, return | ||
? ( // a random number from 0 to 15 | ||
a ^ // unless b is 8, | ||
Math.random() // in which case | ||
* 16 // a random number from | ||
>> a/4 // 8 to 11 | ||
).toString(16) // in hexadecimal | ||
: ( // or otherwise a concatenated string: | ||
[1e7] + // 10000000 + | ||
-1e3 + // -1000 + | ||
-4e3 + // -4000 + | ||
-8e3 + // -80000000 + | ||
-1e11 // -100000000000, | ||
).replace( // replacing | ||
/[018]/g, // zeroes, ones, and eights with | ||
b // random hex digits | ||
) | ||
} | ||
/* jshint ignore:end */ | ||
// UUID | ||
// https://gist.github.com/jed/982883 | ||
/* jshint ignore:start */ | ||
function b( | ||
a // placeholder | ||
){ | ||
return a // if the placeholder was passed, return | ||
? ( // a random number from 0 to 15 | ||
a ^ // unless b is 8, | ||
Math.random() // in which case | ||
* 16 // a random number from | ||
>> a/4 // 8 to 11 | ||
).toString(16) // in hexadecimal | ||
: ( // or otherwise a concatenated string: | ||
[1e7] + // 10000000 + | ||
-1e3 + // -1000 + | ||
-4e3 + // -4000 + | ||
-8e3 + // -80000000 + | ||
-1e11 // -100000000000, | ||
).replace( // replacing | ||
/[018]/g, // zeroes, ones, and eights with | ||
b // random hex digits | ||
) | ||
} | ||
/* jshint ignore:end */ | ||
// Copies properties from an docect to another | ||
function __update(dest, src) { | ||
_.each(src, function(value, key) { | ||
dest[key] = value; | ||
}); | ||
} | ||
// Copies properties from an object to another | ||
function __update(dest, src) { | ||
_.each(src, function(value, key) { | ||
dest[key] = value; | ||
}); | ||
} | ||
// Removes an item from an array | ||
function __remove(array, item) { | ||
var index = _.indexOf(array, item); | ||
if (index != -1) array.splice(index, 1); | ||
} | ||
// Removes an item from an array | ||
function __remove(array, item) { | ||
var index = _.indexOf(array, item); | ||
if (index != -1) array.splice(index, 1); | ||
} | ||
function get(collection, id) { | ||
return _.find(collection, function (doc) { | ||
return doc.id === id; | ||
}); | ||
} | ||
function get(collection, id) { | ||
return _.find(collection, function (doc) { | ||
return doc.id === id; | ||
}); | ||
} | ||
function createId(collection, doc) { | ||
return b(); | ||
} | ||
function createId(collection) { | ||
return b(); | ||
} | ||
function insert(collection, doc) { | ||
var clone = _.clone(doc); | ||
function insert(collection, obj) { | ||
var clone = _.clone(obj); | ||
if (!clone.hasOwnProperty('id')) clone.id = _.createId(collection, doc); | ||
if (!clone.hasOwnProperty('id')) clone.id = _.createId(collection); | ||
collection.push(clone); | ||
collection.push(clone); | ||
return clone; | ||
} | ||
return clone; | ||
} | ||
function update(collection, id, attrs) { | ||
var doc = get(collection, id); | ||
function update(collection, id, attrs) { | ||
var doc = get(collection, id); | ||
if (doc) __update(doc, attrs); | ||
if (doc) __update(doc, attrs); | ||
return doc; | ||
} | ||
return doc; | ||
} | ||
function updateWhere(collection, whereAttrs, attrs) { | ||
var docs = _.where(collection, whereAttrs); | ||
function updateWhere(collection, whereAttrs, attrs) { | ||
var docs = _.where(collection, whereAttrs); | ||
docs.forEach(function(doc) { | ||
__update(doc, attrs); | ||
}); | ||
docs.forEach(function(doc) { | ||
__update(doc, attrs); | ||
}); | ||
return docs; | ||
} | ||
return docs; | ||
} | ||
function remove(collection, id) { | ||
var doc = get(collection, id); | ||
function remove(collection, id) { | ||
var doc = get(collection, id); | ||
__remove(collection, doc); | ||
__remove(collection, doc); | ||
return doc; | ||
} | ||
return doc; | ||
} | ||
function removeWhere(collection, attrs) { | ||
var docs = _.where(collection, attrs); | ||
function removeWhere(collection, attrs) { | ||
var docs = _.where(collection, attrs); | ||
docs.forEach(function(doc) { | ||
__remove(collection, doc); | ||
}); | ||
docs.forEach(function(doc) { | ||
__remove(collection, doc); | ||
}); | ||
return docs; | ||
} | ||
return docs; | ||
} | ||
function set_(lib) { | ||
_ = lib; | ||
} | ||
function mixWith(lib) { | ||
_ = lib; | ||
_.mixin({ | ||
get: get, | ||
createId: createId, | ||
insert: insert, | ||
update: update, | ||
updateWhere: updateWhere, | ||
remove: remove, | ||
removeWhere: removeWhere | ||
}); | ||
} | ||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports.mixWith = mixWith; | ||
} else { | ||
mixWith(root._); | ||
} | ||
})(this); | ||
module.exports = { | ||
set_: set_, | ||
get: get, | ||
createId: createId, | ||
insert: insert, | ||
update: update, | ||
updateWhere: updateWhere, | ||
remove: remove, | ||
removeWhere: removeWhere | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16781
12
286
3
219
5