Skip to content

Commit

Permalink
Minor syntax tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Jun 14, 2016
1 parent 0b41aae commit f04e5d7
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 117 deletions.
103 changes: 49 additions & 54 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2016
* @license BSD-3-Clause
* @link http://haro.rocks
* @version 1.9.10
* @version 1.9.11
*/
"use strict";

Expand Down Expand Up @@ -34,25 +34,25 @@ function cast (input) {
switch (true) {
case input instanceof Map:
result = {};
input.forEach(function (value, key) {
input.forEach((value, key) => {
result[key] = cast(value);
});
break;
case input instanceof Set:
result = [];
input.forEach(function (i) {
input.forEach(i => {
result.push(cast(i));
});
break;
case input instanceof Array:
result = new Set();
input.forEach(function (i) {
input.forEach(i => {
result.add(cast(i));
});
break;
case input instanceof Object:
result = new Map();
Object.keys(input).forEach(function (i) {
Object.keys(input).forEach(i => {
result.set(i, cast(input[i]));
});
break;
Expand Down Expand Up @@ -92,7 +92,7 @@ function keyIndex (key, data, delimiter, pattern) {
result;

if (keys.length > 1) {
result = keys.map(function (i) {
result = keys.map(i => {
return String(data[i]).replace(new RegExp(pattern, "g"), "").toLowerCase();
}).join(delimiter);
} else {
Expand All @@ -103,7 +103,7 @@ function keyIndex (key, data, delimiter, pattern) {
}

function delIndex (index, indexes, delimiter, key, data, pattern) {
index.forEach(function (i) {
index.forEach(i => {
let idx = indexes.get(i),
value = keyIndex(i, data, delimiter, pattern),
o;
Expand All @@ -123,15 +123,15 @@ function delIndex (index, indexes, delimiter, key, data, pattern) {
function createIndexes (records, indexes, key, delimiter, pattern) {
let result = {};

indexes.forEach(function (i) {
indexes.forEach(i => {
result[i] = {};
});

records.forEach(function (i) {
records.forEach(i => {
let lkey = i[key];

if (lkey !== undefined) {
indexes.forEach(function (index) {
indexes.forEach(index => {
let lindex = keyIndex(index, i, delimiter, pattern);

if (result[index][lindex] === undefined) {
Expand Down Expand Up @@ -159,7 +159,7 @@ function each (arg, fn) {

function iterate (obj, fn) {
if (obj instanceof Object) {
Object.keys(obj).forEach(function (i) {
Object.keys(obj).forEach(i => {
fn.call(obj, obj[i], i);
});
} else {
Expand All @@ -169,7 +169,7 @@ function iterate (obj, fn) {

function merge (a, b) {
if (a instanceof Object && b instanceof Object) {
Object.keys(b).forEach(function (i) {
Object.keys(b).forEach(i => {
if (a[i] instanceof Object && b[i] instanceof Object) {
a[i] = merge(a[i], b[i]);
} else if (a[i] instanceof Array && b[i] instanceof Array) {
Expand All @@ -196,17 +196,17 @@ function joinData (id, a, b, key, on, type = "inner") {
let keys = Object.keys(right[0]),
fn;

fn = !reverse ? function (x, i) {
fn = !reverse ? (x, i) => {
return x[on] === i[key];
} : function (x, i) {
} : (x, i) => {
return x[key] === i[on];
};

each(left, function (i) {
each(left, i => {
let comp = {},
c;

c = right.filter(function (x) {
c = right.filter(x => {
return fn(x, i);
});

Expand All @@ -215,17 +215,17 @@ function joinData (id, a, b, key, on, type = "inner") {
errorMsg = "More than one record found on " + i[on];
return false;
} else if (c.length === 1) {
[i, c[0]].forEach(function (x, idx) {
iterate(x, function (v, k) {
[i, c[0]].forEach((x, idx) => {
iterate(x, (v, k) => {
comp[ids[idx] + "_" + k] = v;
});
});
} else if (include) {
iterate(i, function (v, k) {
iterate(i, (v, k) => {
comp[ids[0] + "_" + k] = v;
});

keys.forEach(function (k) {
keys.forEach(k => {
comp[ids[1] + "_" + k] = null;
});
}
Expand Down Expand Up @@ -271,14 +271,14 @@ function patch (ogdata = {}, data = {}, key = "", overwrite = false) {
let result = [];

if (overwrite) {
iterate(ogdata, function (v, k) {
iterate(ogdata, (v, k) => {
if (k !== key && data[k] === undefined) {
result.push({op: "remove", path: "/" + k});
}
});
}

iterate(data, function (v, k) {
iterate(data, (v, k) => {
if (k !== key && ogdata[k] === undefined) {
result.push({op: "add", path: "/" + k, value: v});
} else if (JSON.stringify(ogdata[k]) !== JSON.stringify(v)) {
Expand All @@ -305,7 +305,7 @@ function setIndex (index, indexes, delimiter, key, data, indice, pattern) {
let idx;

if (!indice) {
index.forEach(function (i) {
index.forEach(i => {
let lidx = keyIndex(i, data, delimiter, pattern);

if (lidx !== undefined && lidx !== null) {
Expand All @@ -324,7 +324,7 @@ function setIndex (index, indexes, delimiter, key, data, indice, pattern) {
function toObjekt (arg) {
let result = {};

arg.forEach(function (value, key) {
arg.forEach((value, key) => {
result[key] = value;
});

Expand Down Expand Up @@ -417,7 +417,7 @@ class Haro {
this.request(concatURI(this.uri, null), {
method: "patch",
body: JSON.stringify(data)
}).then(function () {
}).then(() => {
next();
}, defer.reject);
} else {
Expand Down Expand Up @@ -575,7 +575,7 @@ class Haro {
filter (fn) {
let result = [];

this.forEach(function (value, key) {
this.forEach((value, key) => {
if (fn(value, key) === true) {
result.push(tuple(key, value));
}
Expand All @@ -585,7 +585,7 @@ class Haro {
}

forEach (fn, ctx) {
this.data.forEach(function (value, key) {
this.data.forEach((value, key) => {
fn(clone(value), clone(key));
}, ctx);

Expand Down Expand Up @@ -617,7 +617,7 @@ class Haro {
promise = this.offload([[this.id, other.id], this.toArray(null, true), other.toArray(null, true), this.key, on, type], "join");
}

promise.then(function (result) {
promise.then(result => {
if (typeof result === "string") {
defer.reject(new Error(result));
} else {
Expand Down Expand Up @@ -689,7 +689,7 @@ class Haro {
map (fn) {
let result = [];

this.forEach(function (value, key) {
this.forEach((value, key) => {
result.push(fn(value, key));
});

Expand Down Expand Up @@ -797,25 +797,25 @@ class Haro {

cfg.method = cfg.method.toUpperCase();

fetch(input, cfg).then(function (res) {
fetch(input, cfg).then(res => {
let status = res.status,
headers;

if (res.headers._headers) {
headers = {};
Object.keys(res.headers._headers).forEach(function (i) {
Object.keys(res.headers._headers).forEach(i => {
headers[i] = res.headers._headers[i].join(", ");
});
} else {
headers = toObjekt(res.headers);
}

res[res.headers.get("content-type").indexOf("application/json") > -1 ? "json" : "text"]().then(function (arg) {
res[res.headers.get("content-type").indexOf("application/json") > -1 ? "json" : "text"]().then(arg => {
defer[status < 200 || status >= 400 ? "reject" : "resolve"](tuple(arg, status, headers));
}, function (e) {
}, e => {
defer.reject(tuple(e.message, status, headers));
});
}, function (e) {
}, e => {
defer.reject(tuple(e.message, 0, {}));
});

Expand Down Expand Up @@ -894,7 +894,7 @@ class Haro {
if (lkey === null) {
if (this.key) {
if (this.source) {
this.source.split(".").forEach(function (i) {
this.source.split(".").forEach(i => {
xdata = xdata[i] || {};
});
}
Expand Down Expand Up @@ -971,9 +971,7 @@ class Haro {
this.request(luri, {
method: method,
body: JSON.stringify(ldata)
}).then(next, function (err) {
defer.reject(err);
});
}).then(next, defer.reject);
} else {
defer.reject(e);
}
Expand All @@ -982,9 +980,7 @@ class Haro {
this.request(luri, {
method: method,
body: JSON.stringify(ldata)
}).then(next, function (e) {
defer.reject(e);
});
}).then(next, defer.reject);
}
} else {
next();
Expand Down Expand Up @@ -1018,7 +1014,7 @@ class Haro {
let result;

if (frozen) {
result = Object.freeze(this.toArray(null, false).sort(fn).map(function (i) {
result = Object.freeze(this.toArray(null, false).sort(fn).map(i => {
return Object.freeze(i);
}));
} else {
Expand Down Expand Up @@ -1061,7 +1057,7 @@ class Haro {
});

if (deferreds.length > 0) {
Promise.all(deferreds).then(function () {
Promise.all(deferreds).then(() => {
defer.resolve(true);
}, defer.reject);
} else {
Expand All @@ -1081,7 +1077,7 @@ class Haro {

if (this.source) {
try {
this.source.split(".").forEach(function (i) {
this.source.split(".").forEach(i => {
data = data[i];
});
} catch (e) {
Expand All @@ -1094,7 +1090,7 @@ class Haro {
}

this.batch(data, "set").then(defer.resolve, defer.reject);
}, function (e) {
}, e => {
defer.reject(e[0] || e);
});

Expand Down Expand Up @@ -1166,16 +1162,16 @@ class Haro {
let func;

if (frozen) {
func = function (arg) {
func = arg => {
return arg;
};
} else {
func = function (arg) {
func = arg => {
return clone(arg);
};
}

return func(!data ? toObjekt(this) : data.reduce(function (a, b) {
return func(!data ? toObjekt(this) : data.reduce((a, b) => {
a[b[0]] = b[1];

return a;
Expand Down Expand Up @@ -1217,12 +1213,12 @@ class Haro {

if (this.worker) {
obj = new Worker(this.worker);
obj.onerror = function (err) {
obj.onerror = err => {
defer.reject(err);
obj.terminate();
};

obj.onmessage = function (ev) {
obj.onmessage = ev => {
defer.resolve(JSON.parse(ev.data));
obj.terminate();
};
Expand Down Expand Up @@ -1263,16 +1259,15 @@ function factory (data = null, config = {}, indexes = []) {
}

factory.transform = cast;
factory.version = "1.9.10";
factory.version = "1.9.11";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
module.exports = factory;
} else if (typeof define === "function" && define.amd) {
define(function () {
define(() => {
return factory;
});
} else {
global.haro = factory;
}
}(typeof window !== "undefined" ? window : global));
}}(typeof window !== "undefined" ? window : global));
Loading

0 comments on commit f04e5d7

Please sign in to comment.