firestruct
Advanced tools
Comparing version 0.0.20 to 0.0.21
@@ -1,9 +0,9 @@ | ||
"use strict"; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.createCursorList = exports.createCursor = exports.updateValue = exports.getValue = undefined; | ||
var _utilities = require("./utilities"); | ||
var _utilities = require('./utilities'); | ||
@@ -20,6 +20,6 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
var firebaseCursor = function firebaseCursor(fbRoot, path) { | ||
if (path.length === 0) { | ||
return fbRoot; | ||
} | ||
return fbRoot.child(path.join("/")); | ||
if (path.length === 0) { | ||
return fbRoot; | ||
} | ||
return fbRoot.child(path.join('/')); | ||
}; | ||
@@ -31,5 +31,5 @@ | ||
var getValue = function getValue(fb) { | ||
return function () { | ||
return fb.val(); | ||
}; | ||
return function () { | ||
return fb.val(); | ||
}; | ||
}; | ||
@@ -41,6 +41,6 @@ | ||
var createCursor = function createCursor(fbRoot, prePath) { | ||
return function (postPath) { | ||
var path = [].concat(_toConsumableArray(prePath), _toConsumableArray((0, _utilities.unit)(postPath))); | ||
return new Cursor({ fbRoot: fbRoot, path: path }); | ||
}; | ||
return function (postPath) { | ||
var path = [].concat(_toConsumableArray(prePath), _toConsumableArray((0, _utilities.unit)(postPath))); | ||
return new Cursor({ fbRoot: fbRoot, path: path }); | ||
}; | ||
}; | ||
@@ -52,9 +52,10 @@ | ||
var updateValue = function updateValue(fbRoot, path) { | ||
return function (callback) { | ||
var fb = firebaseCursor(fbRoot, path); | ||
var oldValue = getValue(fb)(); | ||
var newValue = (0, _utilities.clean)(callback(oldValue)); | ||
/* const result = */fb.ref().set(newValue); | ||
return createCursor(fbRoot, path); // Return updated cursor | ||
}; | ||
return function (callback) { | ||
console.warn('Firestruct: cursor.set @ ' + path.join('/')); | ||
var fb = firebaseCursor(fbRoot, path); | ||
var oldValue = getValue(fb)(); | ||
var newValue = (0, _utilities.clean)(callback(oldValue)); | ||
/* const result = */fb.ref().set(newValue); | ||
return createCursor(fbRoot, path); // Return updated cursor | ||
}; | ||
}; | ||
@@ -67,3 +68,3 @@ | ||
var getHash = function getHash(fb, path) { | ||
return "[object FbCursor - " + fb.numChildren() + "|" + fb.child("tree").numChildren() + "|" + path.join("/") + "]"; | ||
return ['[object FbCursor - ' + fb.numChildren(), fb.child('tree').numChildren(), path.join('/') + ']'].join('|'); | ||
}; | ||
@@ -79,7 +80,7 @@ | ||
var setValue = function setValue(partialUpdate) { | ||
return function (value) { | ||
return partialUpdate(function () { | ||
return value; | ||
}); | ||
}; | ||
return function (value) { | ||
return partialUpdate(function () { | ||
return value; | ||
}); | ||
}; | ||
}; | ||
@@ -91,8 +92,8 @@ | ||
var createCursorList = function createCursorList(fb, partialCursor) { | ||
return function () { | ||
var keys = Object.keys(fb.val() || {}); | ||
return keys.map(function (key) { | ||
return [key, partialCursor(key)]; | ||
}); | ||
}; | ||
return function () { | ||
var keys = Object.keys(fb.val() || {}); | ||
return keys.map(function (key) { | ||
return [key, partialCursor(key)]; | ||
}); | ||
}; | ||
}; | ||
@@ -108,31 +109,25 @@ | ||
function Cursor(_ref) { | ||
var fbRoot = _ref.fbRoot; | ||
var path = _ref.path; | ||
var fbRoot = _ref.fbRoot, | ||
path = _ref.path; | ||
var fb = firebaseCursor(fbRoot, path); | ||
var partialCursor = createCursor(fbRoot, path); | ||
var partialUpdate = updateValue(fbRoot, path); | ||
var hash = getHash(fb, path); | ||
return Object.assign(this, { | ||
// Integral | ||
value: getValue(fb), | ||
update: partialUpdate, | ||
cursor: partialCursor, | ||
// Convenience | ||
set: setValue(partialUpdate), | ||
cursors: createCursorList(fb, partialCursor), | ||
// Interop | ||
// deref: , // For Omniscient immutability | ||
toString: function toString() { | ||
return hash; | ||
}, // For Ramda memoization | ||
// Depreciated | ||
_fb: fb | ||
}); | ||
var fb = firebaseCursor(fbRoot, path); | ||
var partialCursor = createCursor(fbRoot, path); | ||
var partialUpdate = updateValue(fbRoot, path); | ||
var hash = getHash(fb, path); | ||
return Object.assign(this, { | ||
// Integral | ||
value: getValue(fb), | ||
update: partialUpdate, | ||
cursor: partialCursor, | ||
// Convenience | ||
set: setValue(partialUpdate), | ||
cursors: createCursorList(fb, partialCursor), | ||
// Interop | ||
// deref: , // For Omniscient immutability | ||
toString: function toString() { | ||
return hash; | ||
}, // For Ramda memoization | ||
// Depreciated | ||
_fb: fb | ||
}); | ||
} | ||
@@ -144,3 +139,2 @@ | ||
exports.createCursor = createCursor; | ||
exports.createCursorList = createCursorList; | ||
//# sourceMappingURL=cursor.js.map | ||
exports.createCursorList = createCursorList; |
@@ -1,21 +0,21 @@ | ||
"use strict"; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.trigger = exports.registerHandlers = exports.registerHandler = undefined; | ||
var _firebase = require("firebase"); | ||
var _firebase = require('firebase'); | ||
var _firebase2 = _interopRequireDefault(_firebase); | ||
var _setasap = require("setasap"); | ||
var _setasap = require('setasap'); | ||
var _setasap2 = _interopRequireDefault(_setasap); | ||
var _cursor = require("./cursor"); | ||
var _cursor = require('./cursor'); | ||
var _cursor2 = _interopRequireDefault(_cursor); | ||
var _utilities = require("./utilities"); | ||
var _utilities = require('./utilities'); | ||
@@ -30,13 +30,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var registerHandler = function registerHandler(firebase, action, callback) { | ||
var oldCursor = void 0; | ||
switch (action) { | ||
default: | ||
firebase.on(action, function (fbRoot) { | ||
var path = []; | ||
var newCursor = new _cursor2.default({ fbRoot: fbRoot, path: path }); | ||
callback([newCursor, oldCursor, path]); | ||
oldCursor = newCursor; | ||
}); | ||
return; | ||
} | ||
var oldCursor = void 0; | ||
switch (action) { | ||
default: | ||
firebase.on(action, function (fbRoot) { | ||
var path = []; | ||
var newCursor = new _cursor2.default({ fbRoot: fbRoot, path: path }); | ||
callback([newCursor, oldCursor, path]); | ||
oldCursor = newCursor; | ||
}); | ||
return; | ||
} | ||
}; | ||
@@ -48,5 +48,5 @@ | ||
var registerHandlers = function registerHandlers(firebase, handlers) { | ||
Object.keys(handlers).forEach(function (action) { | ||
return registerHandler(firebase, action, handlers[action]); | ||
}); | ||
Object.keys(handlers).forEach(function (action) { | ||
return registerHandler(firebase, action, handlers[action]); | ||
}); | ||
}; | ||
@@ -58,7 +58,7 @@ | ||
var trigger = function trigger(handlers, args) { | ||
handlers.forEach(function (handler) { | ||
return (0, _setasap2.default)(function () { | ||
return handler.apply(undefined, _toConsumableArray(args)); | ||
}); | ||
handlers.forEach(function (handler) { | ||
return (0, _setasap2.default)(function () { | ||
return handler.apply(undefined, _toConsumableArray(args)); | ||
}); | ||
}); | ||
}; | ||
@@ -70,23 +70,21 @@ | ||
function Structure(url) { | ||
var fb = new _firebase2.default(url); | ||
var handlers = { | ||
swap: [], | ||
auth: [] | ||
}; | ||
registerHandlers(fb, { | ||
value: function value(args) { | ||
return trigger(handlers.swap, args); | ||
} | ||
var fb = new _firebase2.default(url); | ||
var handlers = { | ||
swap: [], | ||
auth: [] | ||
}; | ||
registerHandlers(fb, { | ||
value: function value(args) { | ||
return trigger(handlers.swap, args); | ||
} | ||
}); | ||
var on = function on(actions, handler) { | ||
return (0, _utilities.unit)(actions).map(function (action) { | ||
return handlers[action].push(handler); | ||
}); | ||
var on = function on(actions, handler) { | ||
return (0, _utilities.unit)(actions).map(function (action) { | ||
return handlers[action].push(handler); | ||
}); | ||
}; | ||
return Object.assign(this, { | ||
on: on, | ||
_fb: fb | ||
}); | ||
}; | ||
return Object.assign(this, { | ||
on: on, | ||
_fb: fb | ||
}); | ||
} | ||
@@ -97,3 +95,2 @@ | ||
exports.registerHandlers = registerHandlers; | ||
exports.trigger = trigger; | ||
//# sourceMappingURL=index.js.map | ||
exports.trigger = trigger; |
@@ -1,21 +0,20 @@ | ||
"use strict"; | ||
'use strict'; | ||
/* | ||
* :: * -> [*] | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
/* | ||
* :: * -> [*] | ||
*/ | ||
var unit = function unit(v) { | ||
if (Array.isArray(v)) { | ||
return v; | ||
} | ||
if (v === undefined || v === "") { | ||
return []; | ||
} | ||
return [v]; | ||
if (Array.isArray(v)) { | ||
return v; | ||
} | ||
if (v === undefined || v === '') { | ||
return []; | ||
} | ||
return [v]; | ||
}; | ||
@@ -27,17 +26,16 @@ | ||
var clean = function clean(o) { | ||
var output = {}; | ||
Object.keys(o).forEach(function (key) { | ||
var value = o[key]; | ||
if (value && (typeof value === "undefined" ? "undefined" : _typeof(value)) === "object") { | ||
value = clean(value); | ||
} | ||
if (value !== undefined) { | ||
output[key] = value; | ||
} | ||
}); | ||
return output; | ||
var output = {}; | ||
Object.keys(o).forEach(function (key) { | ||
var value = o[key]; | ||
if (value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') { | ||
value = clean(value); | ||
} | ||
if (value !== undefined) { | ||
output[key] = value; | ||
} | ||
}); | ||
return output; | ||
}; | ||
exports.unit = unit; | ||
exports.clean = clean; | ||
//# sourceMappingURL=utilities.js.map | ||
exports.clean = clean; |
{ | ||
"name": "firestruct", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"description": "Minimal immutable Firebase API", | ||
@@ -11,3 +11,5 @@ "author": { | ||
"scripts": { | ||
"prepublish": "grunt" | ||
"test": "semistandard | snazzy && mocha", | ||
"build": "yarn test && rm -rf dist && babel src --out-dir dist", | ||
"prepublish": "yarn build" | ||
}, | ||
@@ -24,10 +26,25 @@ "repository": { | ||
"devDependencies": { | ||
"babel-core": "^6.7.7", | ||
"babel-eslint": "^6.0.4", | ||
"babel-preset-es2015": "^6.6.0", | ||
"eslint": "^2.8.0", | ||
"grunt": "^1.0.1", | ||
"grunt-babel": "^6.0.0", | ||
"load-grunt-tasks": "^3.5.0" | ||
"babel-cli": "^6.22.2", | ||
"babel-core": "^6.22.1", | ||
"babel-eslint": "^7.1.1", | ||
"babel-preset-es2015": "^6.22.0", | ||
"babel-preset-stage-0": "^6.22.0", | ||
"eslint": "^3.15.0", | ||
"mocha": "^3.2.0", | ||
"semistandard": "^9.2.1", | ||
"snazzy": "^6.0.0" | ||
}, | ||
"babel": { | ||
"presets": [ | ||
"es2015", | ||
"stage-0" | ||
] | ||
}, | ||
"semistandard": { | ||
"parser": "babel-eslint", | ||
"env": { | ||
"browser": true, | ||
"mocha": true | ||
} | ||
} | ||
} |
@@ -1,10 +0,6 @@ | ||
"use strict"; | ||
import { | ||
unit, | ||
clean | ||
} from "./utilities"; | ||
unit, | ||
clean | ||
} from './utilities'; | ||
/** | ||
@@ -14,3 +10,2 @@ * Integral functions | ||
/* | ||
@@ -20,9 +15,8 @@ * Select Firebase child (by array) | ||
const firebaseCursor = (fbRoot, path) => { | ||
if (path.length === 0) { | ||
return fbRoot; | ||
} | ||
return fbRoot.child(path.join("/")); | ||
if (path.length === 0) { | ||
return fbRoot; | ||
} | ||
return fbRoot.child(path.join('/')); | ||
}; | ||
/* | ||
@@ -33,3 +27,2 @@ * Get a value from | ||
/* | ||
@@ -39,7 +32,6 @@ * Create a cursor for a child path | ||
const createCursor = (fbRoot, prePath) => postPath => { | ||
const path = [ ...prePath, ...unit(postPath) ]; | ||
return new Cursor({ fbRoot, path }); | ||
const path = [ ...prePath, ...unit(postPath) ]; | ||
return new Cursor({ fbRoot, path }); | ||
}; | ||
/* | ||
@@ -49,10 +41,10 @@ * Update a value | ||
const updateValue = (fbRoot, path) => callback => { | ||
const fb = firebaseCursor(fbRoot, path); | ||
const oldValue = getValue(fb)(); | ||
const newValue = clean(callback(oldValue)); | ||
/* const result = */ fb.ref().set(newValue); | ||
return createCursor(fbRoot, path); // Return updated cursor | ||
console.warn('Firestruct: cursor.set @ ' + path.join('/')); | ||
const fb = firebaseCursor(fbRoot, path); | ||
const oldValue = getValue(fb)(); | ||
const newValue = clean(callback(oldValue)); | ||
/* const result = */ fb.ref().set(newValue); | ||
return createCursor(fbRoot, path); // Return updated cursor | ||
}; | ||
/* | ||
@@ -62,11 +54,8 @@ * Build a hash (for memoization). | ||
*/ | ||
const getHash = (fb, path) => "[object FbCursor - " | ||
+ fb.numChildren() | ||
+ "|" | ||
+ fb.child("tree").numChildren() | ||
+ "|" | ||
+ path.join("/") | ||
+ "]"; | ||
const getHash = (fb, path) => [ | ||
'[object FbCursor - ' + fb.numChildren(), | ||
fb.child('tree').numChildren(), | ||
path.join('/') + ']' | ||
].join('|'); | ||
/** | ||
@@ -76,3 +65,2 @@ * Convenience functions | ||
/* | ||
@@ -83,3 +71,2 @@ * Synchronously set a value | ||
/* | ||
@@ -89,10 +76,8 @@ * Create an array of [key, cursor] arrays | ||
const createCursorList = (fb, partialCursor) => () => { | ||
const keys = Object.keys(fb.val() || {}); | ||
return keys.map( | ||
key => ([key, partialCursor(key)]) | ||
); | ||
const keys = Object.keys(fb.val() || {}); | ||
return keys.map( | ||
key => ([key, partialCursor(key)]) | ||
); | ||
}; | ||
/** | ||
@@ -102,3 +87,2 @@ * Public API | ||
/* | ||
@@ -108,35 +92,28 @@ * Prototype cursor instance | ||
function Cursor ({ fbRoot, path }) { | ||
const fb = firebaseCursor(fbRoot, path); | ||
const partialCursor = createCursor(fbRoot, path); | ||
const partialUpdate = updateValue(fbRoot, path); | ||
const hash = getHash(fb, path); | ||
return Object.assign(this, { | ||
// Integral | ||
value: getValue(fb), | ||
update: partialUpdate, | ||
cursor: partialCursor, | ||
// Convenience | ||
set: setValue(partialUpdate), | ||
cursors: createCursorList(fb, partialCursor), | ||
// Interop | ||
// deref: , // For Omniscient immutability | ||
toString: () => hash, // For Ramda memoization | ||
// Depreciated | ||
_fb: fb | ||
}); | ||
const fb = firebaseCursor(fbRoot, path); | ||
const partialCursor = createCursor(fbRoot, path); | ||
const partialUpdate = updateValue(fbRoot, path); | ||
const hash = getHash(fb, path); | ||
return Object.assign(this, { | ||
// Integral | ||
value: getValue(fb), | ||
update: partialUpdate, | ||
cursor: partialCursor, | ||
// Convenience | ||
set: setValue(partialUpdate), | ||
cursors: createCursorList(fb, partialCursor), | ||
// Interop | ||
// deref: , // For Omniscient immutability | ||
toString: () => hash, // For Ramda memoization | ||
// Depreciated | ||
_fb: fb | ||
}); | ||
} | ||
export default Cursor; | ||
export { | ||
getValue, | ||
updateValue, | ||
createCursor, | ||
createCursorList | ||
getValue, | ||
updateValue, | ||
createCursor, | ||
createCursorList | ||
}; |
@@ -1,11 +0,8 @@ | ||
"use strict"; | ||
import Firebase from "firebase"; | ||
import setAsap from "setasap"; | ||
import Cursor from "./cursor"; | ||
'use strict'; | ||
import Firebase from 'firebase'; | ||
import setAsap from 'setasap'; | ||
import Cursor from './cursor'; | ||
import { | ||
unit | ||
} from "./utilities"; | ||
unit | ||
} from './utilities'; | ||
/* | ||
@@ -15,16 +12,15 @@ * Register event handler | ||
const registerHandler = (firebase, action, callback) => { | ||
let oldCursor; | ||
switch (action) { | ||
let oldCursor; | ||
switch (action) { | ||
default: | ||
firebase.on(action, fbRoot => { | ||
const path = []; | ||
const newCursor = new Cursor({ fbRoot, path }); | ||
callback([ newCursor, oldCursor, path ]); | ||
oldCursor = newCursor; | ||
}); | ||
return; | ||
} | ||
firebase.on(action, fbRoot => { | ||
const path = []; | ||
const newCursor = new Cursor({ fbRoot, path }); | ||
callback([ newCursor, oldCursor, path ]); | ||
oldCursor = newCursor; | ||
}); | ||
return; | ||
} | ||
}; | ||
/* | ||
@@ -34,8 +30,7 @@ * Register a bunch of event types | ||
const registerHandlers = (firebase, handlers) => { | ||
Object.keys(handlers).forEach( | ||
action => registerHandler(firebase, action, handlers[action]) | ||
); | ||
Object.keys(handlers).forEach( | ||
action => registerHandler(firebase, action, handlers[action]) | ||
); | ||
}; | ||
/* | ||
@@ -45,8 +40,7 @@ * Async trigger a bunch of event handlers | ||
const trigger = (handlers, args) => { | ||
handlers.forEach(handler => setAsap( | ||
() => handler(...args) | ||
)); | ||
handlers.forEach(handler => setAsap( | ||
() => handler(...args) | ||
)); | ||
}; | ||
/* | ||
@@ -56,28 +50,24 @@ * Prototype structure instance | ||
function Structure (url) { | ||
const fb = new Firebase(url); | ||
const handlers = { | ||
swap: [], | ||
auth: [] | ||
}; | ||
registerHandlers(fb, { | ||
value: args => trigger(handlers.swap, args) | ||
}); | ||
const on = (actions, handler) => unit(actions).map( | ||
action => handlers[action].push(handler) | ||
); | ||
return Object.assign(this, { | ||
on, | ||
_fb: fb | ||
}); | ||
const fb = new Firebase(url); | ||
const handlers = { | ||
swap: [], | ||
auth: [] | ||
}; | ||
registerHandlers(fb, { | ||
value: args => trigger(handlers.swap, args) | ||
}); | ||
const on = (actions, handler) => unit(actions).map( | ||
action => handlers[action].push(handler) | ||
); | ||
return Object.assign(this, { | ||
on, | ||
_fb: fb | ||
}); | ||
} | ||
export default Structure; | ||
export { | ||
registerHandler, | ||
registerHandlers, | ||
trigger | ||
registerHandler, | ||
registerHandlers, | ||
trigger | ||
}; |
@@ -1,4 +0,1 @@ | ||
"use strict"; | ||
/* | ||
@@ -8,12 +5,11 @@ * :: * -> [*] | ||
const unit = v => { | ||
if (Array.isArray(v)) { | ||
return v; | ||
} | ||
if (v === undefined || v === "") { | ||
return []; | ||
} | ||
return [v]; | ||
if (Array.isArray(v)) { | ||
return v; | ||
} | ||
if (v === undefined || v === '') { | ||
return []; | ||
} | ||
return [v]; | ||
}; | ||
/* | ||
@@ -23,19 +19,18 @@ * Recursively remove undefined values | ||
const clean = o => { | ||
const output = {}; | ||
Object.keys(o).forEach(key => { | ||
let value = o[key]; | ||
if (value && typeof value === "object") { | ||
value = clean(value); | ||
} | ||
if (value !== undefined) { | ||
output[key] = value; | ||
} | ||
}); | ||
return output; | ||
const output = {}; | ||
Object.keys(o).forEach(key => { | ||
let value = o[key]; | ||
if (value && typeof value === 'object') { | ||
value = clean(value); | ||
} | ||
if (value !== undefined) { | ||
output[key] = value; | ||
} | ||
}); | ||
return output; | ||
}; | ||
export { | ||
unit, | ||
clean | ||
unit, | ||
clean | ||
}; |
111761
9
13
426