Skip to content

Commit

Permalink
chore: use rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
thgreasi committed May 14, 2016
1 parent acae437 commit 5e0f1df
Show file tree
Hide file tree
Showing 26 changed files with 688 additions and 2,305 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [ "es2015-rollup" ]
}
25 changes: 25 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"modules": true
}
},
"extends": "eslint:recommended",
"env": {
"node": true,
"mocha": true
},
"rules":{
"no-console": 0
},
"globals": {
"localforage": true,
"Promise": true,
"console": true,
"self": true,
"System": true
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
bower_components
node_modules
components
4 changes: 3 additions & 1 deletion .jscsrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"esnext": true,
"disallowSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true
},
Expand All @@ -25,5 +26,6 @@
"validateQuoteMarks": {
"escape": true,
"mark": "'"
}
},
"validateIndentation": 4
}
8 changes: 6 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"curly": true,
"eqeqeq": true,
"eqnull": true,
"esnext": false,
"esversion": 6,
"immed": true,
"latedef": true,
"newcap": true,
Expand All @@ -20,9 +20,13 @@
"validthis": true,

"globals": {
"console": true,
"define": true,
"localforage": true,
"module": true,
"require": true
"Promise": true,
"require": true,
"self": true,
"System": true
}
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "4.0"
- "5.0"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Adds startsWith method to [localForage](https://github.com/mozilla/localForage).

## Requirements

* [localForage](https://github.com/mozilla/localForage) v1.2.1+
* [localForage](https://github.com/mozilla/localForage) v1.4.0+
* for earlier versions of localforage, please use the v1.1.x releases

## Installation
`npm i localforage-startswith`
13 changes: 3 additions & 10 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "localforage-startswith",
"version": "1.1.0",
"main": [
"src/localforage-startsWith.js"
"dist/localforage-startswith.js"
],
"ignore": [
".travis.yml",
Expand All @@ -12,20 +12,13 @@
"Gemfile.lock",
"Rakefile",
"LICENSE",
"build*",
"docs*",
"examples*",
"test*",
"site*"
],
"dependencies": {
"localforage": ">=1.2.1"
},
"devDependencies": {
"es6-promise": "~1.0.0",
"requirejs": "~2.1.10",
"mocha": "~1.18.2",
"expect": "~0.3.1",
"assert": "~0.1.0",
"modernizr": "~2.8.1"
"localforage": ">=1.4.0"
}
}
11 changes: 0 additions & 11 deletions component.json

This file was deleted.

179 changes: 179 additions & 0 deletions dist/localforage-startswith.es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import localforage from 'localforage';

function getSerializerPromise(localForageInstance) {
if (getSerializerPromise.result) {
return getSerializerPromise.result;
}
if (!localForageInstance || typeof localForageInstance.getSerializer !== 'function') {
Promise.reject(new Error('localforage.getSerializer() was not available! ' + 'localforage v1.4+ is required!'));
}
getSerializerPromise.result = localForageInstance.getSerializer();
return getSerializerPromise.result;
}

function executeCallback(promise, callback) {
if (callback) {
promise.then(function (result) {
callback(null, result);
}, function (error) {
callback(error);
});
}
}

function getItemKeyValue(key, callback) {
var localforageInstance = this;
var promise = localforageInstance.getItem(key).then(function (value) {
return {
key: key,
value: value
};
});
executeCallback(promise, callback);
return promise;
}

function getIDBKeyRange() {
/* global IDBKeyRange, webkitIDBKeyRange, mozIDBKeyRange */
if (typeof IDBKeyRange !== 'undefined') {
return IDBKeyRange;
}
if (typeof webkitIDBKeyRange !== 'undefined') {
return webkitIDBKeyRange;
}
if (typeof mozIDBKeyRange !== 'undefined') {
return mozIDBKeyRange;
}
}

var idbKeyRange = getIDBKeyRange();

function startsWithIndexedDB(prefix, callback) {
var localforageInstance = this;
var promise = new Promise(function (resolve, reject) {
localforageInstance.ready().then(function () {
// Thanks https://hacks.mozilla.org/2014/06/breaking-the-borders-of-indexeddb/
var dbInfo = localforageInstance._dbInfo;
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);

var keyRangeValue = idbKeyRange.bound(prefix, prefix + 'uffff', false, false);

var result = {};
var req = store.openCursor(keyRangeValue);
req.onsuccess = function () /*event*/{
var cursor = req.result; // event.target.result;

if (cursor) {
var value = cursor.value;
if (value === undefined) {
value = null;
}

result[cursor.key] = value;

cursor.continue();
} else {
resolve(result);
}
};

req.onerror = function () /*event*/{
reject(req.error);
};
}).catch(reject);
});
executeCallback(promise, callback);
return promise;
}

function startsWithWebsql(prefix, callback) {
var localforageInstance = this;
var promise = new Promise(function (resolve, reject) {
localforageInstance.ready().then(function () {
return getSerializerPromise(localforageInstance);
}).then(function (serializer) {
var dbInfo = localforageInstance._dbInfo;
dbInfo.db.transaction(function (t) {
t.executeSql('SELECT * FROM ' + dbInfo.storeName + ' WHERE (key LIKE ?)', [prefix + '%'], function (t, results) {

var result = {};

var rows = results.rows;
for (var i = 0, len = rows.length; i < len; i++) {
var item = rows.item(i);
var value = item.value;

// Check to see if this is serialized content we need to
// unpack.
if (value) {
value = serializer.deserialize(value);
}

result[item.key] = value;
}

resolve(result);
}, function (t, error) {
reject(error);
});
});
}).catch(reject);
});
executeCallback(promise, callback);
return promise;
}

function startsWithGeneric(prefix, callback) {
var localforageInstance = this;
var promise = new Promise(function (resolve, reject) {
localforageInstance.keys().then(function (keys) {

var itemPromises = [];

var prefixLength = prefix.length;
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i];

if (key.slice(0, prefixLength) === prefix) {
itemPromises.push(getItemKeyValue.call(localforageInstance, key));
}
}

Promise.all(itemPromises).then(function (keyValuePairs) {
var result = {};
for (var i = 0, len = keyValuePairs.length; i < len; i++) {
var keyValuePair = keyValuePairs[i];

result[keyValuePair.key] = keyValuePair.value;
}
resolve(result);
}).catch(reject);
}).catch(reject);
});
executeCallback(promise, callback);
return promise;
}

function localforageStartsWith(prefix, callback) {
var localforageInstance = this;
var currentDriver = localforageInstance.driver();

if (currentDriver === localforageInstance.INDEXEDDB) {
return startsWithIndexedDB.call(localforageInstance, prefix, callback);
} else if (currentDriver === localforageInstance.WEBSQL) {
return startsWithWebsql.call(localforageInstance, prefix, callback);
} else {
return startsWithGeneric.call(localforageInstance, prefix, callback);
}
}

function extendPrototype(localforage) {
var localforagePrototype = Object.getPrototypeOf(localforage);
if (localforagePrototype) {
localforagePrototype.startsWith = localforageStartsWith;
}
}

var extendPrototypeResult = extendPrototype(localforage);

export { startsWithGeneric, localforageStartsWith, extendPrototype, extendPrototypeResult };
Loading

0 comments on commit 5e0f1df

Please sign in to comment.