Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

collectionize

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

collectionize - npm Package Compare versions

Comparing version 0.3.3 to 1.0.0

94

collectionize.js

@@ -23,3 +23,2 @@ (function () {

var mockStorage = {};
var _ = safeRequire('_', 'lodash');

@@ -69,22 +68,2 @@

function storageSet(name, data) {
name = Collectionize.localStoragePrefix + name;
if (isBrowser) {
window.localStorage.setItem(name, data);
} else {
mockStorage[name] = data;
}
}
function storageGet(name) {
name = Collectionize.localStoragePrefix + name;
if (isBrowser) {
return window.localStorage.getItem(name);
} else {
return mockStorage[name];
}
}
function Collectionize(name, self) {

@@ -96,2 +75,3 @@ self = self || {};

self.name = name; // Used for localStorage property naming.
self.idIndex = [];

@@ -157,2 +137,5 @@ nativeEach(LODASH_METHODS, function (methodName) {

self.db[self.db.length] = obj;
if (obj && obj.id) {
self.idIndex[obj.id + ''] = obj;
}
self.trigger('added', obj);

@@ -178,6 +161,3 @@

nativeEach(matches, function (match) {
_.extend(match, obj);
self.trigger('beforeUpdate', match);
output[output.length] = match;
self.trigger('updated', match);
_update(match, obj, output);
});

@@ -191,5 +171,37 @@ } else {

function _update(match, obj, output) {
_.extend(match, obj);
self.trigger('beforeUpdate', match);
if (output) {
output[output.length] = match;
}
if (obj && obj.id) {
self.idIndex[obj.id + ''] = obj;
}
self.trigger('updated', match);
}
self.getById = function (id) {
return self.idIndex[id + ''];
};
self.updateById = function (obj) {
if (!obj.id) {
return self.update(obj);
}
var existing = self.getById(obj.id);
if (existing) {
_update(existing, obj);
} else {
self.add(obj);
}
};
self.remove = function (query) {
var removed = _.remove(self.db, query);
nativeEach(removed, function (obj) {
if (obj && obj.id) {
self.idIndex[obj.id + ''] = null;
}
self.trigger('removed', obj);

@@ -202,2 +214,3 @@ });

self.db = db || [];
self.idIndex = [];
self.trigger('flushed');

@@ -210,31 +223,2 @@ };

self.clientSave = function () {
var data = [];
self.each(function (item, index) {
data[index] = {};
nativeEach(item, function (value, key) {
if (_.isFunction(value)) {
value = '(' + value + ');';
}
// Drop DOM elements, since they don't work with JSON.stringify.
if (!_.isElement(value)) {
data[index][key] = value;
}
});
});
storageSet(self.name, JSON.stringify(data));
};
self.clientLoad = function () {
var data = storageGet(self.name);
try {
return JSON.parse(data);
} catch (e) {
self.trigger('parseError', data, e);
return [];
}
};
// Aliases

@@ -250,4 +234,2 @@

Collectionize.localStoragePrefix = 'Collectionize.';
// Expose

@@ -254,0 +236,0 @@

{
"name": "collectionize",
"description": "A lightweight JS model/collection library.",
"version": "0.3.3",
"version": "1.0.0",
"repository": "https://github.com/andrewchilds/collectionize.git",

@@ -6,0 +6,0 @@ "author": {

@@ -54,32 +54,23 @@ # Collectionize

#### clientSave()
#### flush(), flush(newCollection)
Save the collection to localStorage. Assumes localStorage exists, meaning there is no bundled polyfill. If the collection includes DOM elements, those are dropped, and functions are converted to strings.
Flush out and optionally replace the collection.
```js
Things.clientSave()
Things.flush()
```
#### clientLoad()
#### get
Load the collection from localStorage. Assumes localStorage exists, meaning there is no bundled polyfill.
Alias for the Lodash `find` method.
```js
Things.clientSave()
Things.flush()
Things.db = Things.clientLoad()
```
#### getById(id)
#### flush(), flush(newCollection)
Uses an ID-based index to get a single object. This can be faster than `get` for larger collections.
Flush out and optionally replace the collection.
```js
Things.flush()
Things.getById(123);
// { id: 123, color: 'green', shape: 'triangle' }
```
#### get
Alias for the Lodash `find` method.
#### incr(query, property)

@@ -147,2 +138,11 @@

#### updateById(obj)
Searches the ID index for a matching object and updates it, otherwise adds it to the collection. This can be faster than `update` for larger collections.
```js
// Change color to 'red' for object with id '2'
Things.updateById({ id: 2, color: 'red' });
```
### Lodash Methods

@@ -204,8 +204,8 @@

#### `deleted`
#### `removed`
Do something after an object has been deleted from the collection, such as delete the object on the server.
Do something after an object has been removed from the collection.
```js
Things.on('deleted', function (thing) {
Things.on('removed', function (thing) {
$.ajax({ url: '/thing/' + thing.id, type: 'DELETE' });

@@ -218,4 +218,4 @@ });

```sh
npm install
npm test
yarn
yarn test
```

@@ -225,2 +225,2 @@

MIT. Copyright © 2016 Andrew Childs.
MIT. Copyright © 2017 Andrew Childs.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc