So, I have some string data displayed in browser using python-node js express.
Data is something like this:
some_logs abcdata_start,ds0,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,ds1,06-01-2016,32,42,52,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end,some_logs,abcdata_start,ds0,06-01-2016,13,14,15,06-01-2015,18,19,10,05-01-2015,10,11,12,ds1,06-01-2016,33,43,53,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end
I want to convert this data into json such that:
"intervals": {
"06-01-2016": {
"ds0": {
"0": [
3, 4, 5
],
"1": [
13,14,15
]
},
"ds1": {
"0": [
32, 42, 52
],
"1": [
33, 43, 53
]
}
},
"06-01-2015": {
filling values in similar way as above
}
}
I've spent some time on the question and I'm not sure my solution is optimal but it works :
var str = 'some_logs abcdata_start,ds0,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,ds1,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end,some_logs,abcdata_start,ds0,06-01-2016,13,14,15,06-01-2015,18,19,10,05-01-2015,10,11,12,ds1,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end';
var indexes = [];
var ds = {};
str.split(',').forEach((el, i) => {
if (el.includes("start") || el.includes("end"))
indexes.push(i);
});
indexes = indexes.reduce((acc, el, i, arr) => {
if (i % 2 == 0)
acc.push([el, arr[i+1]]);
return acc;
}, []);
indexes.forEach(arr => {
var tmp = str.split(',').slice(arr[0] + 1, arr[1]);
var tmpIndex = "";
tmp.forEach(val => {
if (val.length === 3 && val.includes("ds"))
tmpIndex = val;
else
ds[tmpIndex]
? ds[tmpIndex].push(val)
: ds[tmpIndex] = [val];
});
});
Object.keys(ds).forEach(key => {
var tmpIndex = "";
ds[key] = ds[key].reduce((acc, val) => {
if (val.match(/[0-9]{2}-[0-9]{2}-[0-9]{4}/) !== null)
tmpIndex = val;
else
acc[tmpIndex]
? acc[tmpIndex].push(val)
: acc[tmpIndex] = [val]
return acc;
}, {});
Object.keys(ds[key]).forEach(dateKey => {
ds[key][dateKey] = ds[key][dateKey].reduce((acc, val, i, arr) => {
if (i % 3 == 0)
acc.push([val, arr[i+1], arr[i+2]]);
return acc;
}, [])
});
});
var res = {};
Object.keys(ds).forEach(dsNum => {
Object.keys(ds[dsNum]).forEach(date => {
if (res.hasOwnProperty(date)) {
res[date][dsNum]
? res[date][dsNum].push(ds[dsNum][date])
: res[date][dsNum] = [ds[dsNum][date]];
} else {
res[date] = {[dsNum]: [ds[dsNum][date]]}
}
})
})
Object.keys(res).forEach(date => {
Object.keys(res[date]).forEach(ds => {
res[date][ds] = res[date][ds][0];
})
})
EDIT
Short version, the same, but with a lot of ugly things, tricks and nested ternary :
var str = 'some_logs abcdata_start,ds0,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,ds1,06-01-2016,32,42,52,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end,some_logs,abcdata_start,ds0,06-01-2016,13,14,15,06-01-2015,18,19,10,05-01-2015,10,11,12,ds1,06-01-2016,33,43,53,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end', indexes = [], ds = {}, res = {};
str.split(',').forEach((el, i) => (el.includes("start") || el.includes("end")) ? indexes.push(i) : null);
indexes = indexes.reduce((acc, el, i, arr) => (i % 2 == 0) ? acc.concat([[el, arr[i+1]]]) : acc, []);
indexes.forEach((arr, tmpIndex = "") => str.split(',').slice(arr[0] + 1, arr[1]).forEach(val => (val.length === 3 && val.includes("ds")) ? tmpIndex = val : ds[tmpIndex] ? ds[tmpIndex].push(val) : ds[tmpIndex] = [val]));
Object.keys(ds).forEach((key, tmpIndex = "") => {
ds[key] = ds[key].reduce((acc, val) => ((val.match(/[0-9]{2}-[0-9]{2}-[0-9]{4}/) !== null) ? tmpIndex = val : acc[tmpIndex] ? acc[tmpIndex].push(val) : acc[tmpIndex] = [val]) == undefined ? acc : acc, {});
Object.keys(ds[key]).forEach(dateKey => ds[key][dateKey] = ds[key][dateKey].reduce((acc, val, i, arr) => (i % 3 == 0) ? acc.concat([[val, arr[i+1], arr[i+2]]]) : acc, []));
});
Object.keys(ds).forEach(dsNum => Object.keys(ds[dsNum]).forEach(date => (res.hasOwnProperty(date)) ? res[date][dsNum] ? res[date][dsNum].push(ds[dsNum][date]) : res[date][dsNum] = [ds[dsNum][date]] : res[date] = {[dsNum]: [ds[dsNum][date]]}));
Object.keys(res).forEach(date => Object.keys(res[date]).forEach(ds => res[date][ds] = res[date][ds][0]));
Hope it helps,
Best regards,
You can use JSON.parse() for converting the json string to JSON
example
var jsonstring = '{'key1':'value1','key2':'value2'}';
var JSondata = JSON.parse(jsonstring);
console.log(JSondata);
and now you can see that your log will be in JSON form
{
'key1':'value1',
'key2':'value2'
}
hope this help u
Related
This is part of code (code not mine, I can't understand how it's working, maybe it's called Promise, but I'm not sure).
m = {
mounted: function() {
var e = this;
this.$bus.on("buff-event", (function(t) {
e.buff(t)
}))
},
methods: {
buff: function(e) {
var t = this;
this.$bus.emit("bfceebecbb-change", "Motivating...");
var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
this.requestFaker.fetch(n).then((function(i) {
i = (i = i.filter((function(t) {
return t.requestMethod == e
}))[0]).responseData.filter((function(e) {
return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
})), n = [], _.forEach(i, (function(e) {
n.push('{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
})), n.length ? (n = "[" + n.join(",") + "]", t.requestFaker.fetch(n).then((function() {
t.$bus.emit("bfceebecbb-change", "Motivation success")
}))) : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
}))
}
}
},
This code collecting data of users, store in to array and then pass it to the server. So it sending big array of all users in one request (it pushing all entries to array in this part n.push('{"__class__":"ServerRequest"...).
What I want to achieve is to send requests for each user one by one with some delays (for example 1 second delay between requests).
I've tried many ways to achieve it, but all unsuccessfully, I'm lack of knowledge about this programming language.
I've tried to use setTimeout functions in different ways, but all the time unsuccessfully:
methods: {
buff: function(e) {
var t = this;
this.$bus.emit("bfceebecbb-change", "Motivating...");
var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
this.requestFaker.fetch(n).then((function(i) {
i = (i = i.filter((function(t) {
return t.requestMethod == e
}))[0]).responseData.filter((function(e) {
return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
})), n = [], _.forEach(i, (function(e) {
n.push('{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
})), n.length ?
setTimeout((function() {
(setTimeout((function() {n = "[" + n.join(",") + "]"}), 1000) , t.requestFaker.fetch(n).then((function() {
t.$bus.emit("bfceebecbb-change", "Motivation success")
})))}), 1000) : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
}))
}
}
Also tried like this:
methods: {
buff: function(e) {
var t = this;
this.$bus.emit("bfceebecbb-change", "Motivating...");
var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
this.requestFaker.fetch(n).then((function(i) {
i = (i = i.filter((function(t) {
return t.requestMethod == e
}))[0]).responseData.filter((function(e) {
return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
})), n = [], _.forEach(i, (function(e) {
n.push('{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
})), n.length ?
setTimeout((function() {
(n = "[" + n.join(",") + "]", t.requestFaker.fetch(n).then((function() {
t.$bus.emit("bfceebecbb-change", "Motivation success")
})))}), 1000) : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
}))
}
}
UPDATED
As per comment asked what is %%requestId%% - I found this part:
{
key: "fetch",
value: function(e) {
function t(t) {
return e.apply(this, arguments)
}
return t.toString = function() {
return e.toString()
}, t
}((function(e) {
for (var t = e;
(t = e.replace("%%requestId%%", this.requestId)) !== e;) e = t, this.incRequestId();
return fetch(gameVars.gatewayUrl, this.getHeadForFetchQuery(e)).then((function(e) {
return e.json()
}))
}))
}
try this
in general, forEach should be replaced with for..of loop with using async/await helper function which will delay the request (in await delaySome(1000); part)
it should send data in the same format, and if it fails at the server, that's probably where it needs tweaking.. or everywhere else..
m = {
mounted: function() {
var e = this;
this.$bus.on("buff-event", function(t) {
e.buff(t);
});
},
methods: {
buff: function(e) {
var t = this;
function delaySome(delay) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, delay);
});
}
this.$bus.emit("bfceebecbb-change", "Motivating...");
var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
this.requestFaker.fetch(n).then(async function(i) {
(i = (i = i.filter(function(t) {
return t.requestMethod == e;
})[0]).responseData.filter(function(e) {
return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted);
})),
(n = []);
if (i.length) {
for (const e of i) {
await delaySome(1000);
t.requestFaker.fetch('[{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}]').then(function() {
t.$bus.emit("bfceebecbb-change", "Motivation success");
})
}
} else {
t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
}
});
},
},
};
Why does my code not work correctly in the Safari browser on Iphone 6 ios 10 and above. In the support table, all values used in the code are supported! The code works everywhere except this device. For my data i use Wilddog it same work as Firebase. Array look like {name: 'Some', like: 2, fav_count: 5 }, {name: 'Some2', like: 3, fav_count: 4 }, {name: 'Some3', like: 3, fav_count: 4 }. Something wrong going on sort() function;
var obj = $wilddogObject(ref); // get data from server
obj.$loaded()
.then(function(data) {
var _extends = Object.assign || function(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } }
return target; };
var moments = _.map(data, function(val, id) {
return _extends({}, val, {
id: id
});
});
$scope.ratePlaces = _.map(moments, function(place) {
// console.log(place);
if (place != null) {
if (place.like != undefined) {
function rating(like, favs) {
var parseFavs = parseInt(favs);
var parseLike = parseInt(like);
var newRate = parseLike * 0.5 + parseFavs;
return newRate;
}
place.rate = rating(place.like, place.fav_count);
}
}
return place
})
console.log($scope.ratePlaces);
list($scope.ratePlaces)
})
function list(r) {
r.shift();
r.shift();
r.shift();
r.sort((a, b) => {
if (a.rate != null || a.rate != undefined || a.rate != 'daylife' || !a.name) {
return a.rate - b.rate;
} else {}
})
r.sort((a, b) => {
if (a.rate != null || a.rate != undefined || a.rate != 'daylife' || !a.name) {
let rateA = a.rate;
let rateB = b.rate;
if (rateA < rateB) {
return -1;
}
if (rateA > rateB) {
return 1;
}
return 0;
}
});
$scope.posts = r.reverse();
I have a filter script that ads a value to my url trough a set of checkboxes. Each checkbox group can only ad one value.
First click on a checkboxgroup looks like this:
/Network-CAT-cables?fss=yellow
Where the fss=yellow is the added parameter. A click on another chekcbox group would look like this:
/Network-CAT-cables?fss=yellow+cat6
If I check another checkbox in one of the above groups it replaces the previous value.
Now i need to add a parameter that looks like this:
&sps=d211av450240_2.0
If I set that as a value in the checkbox it kinda work. It ads the parameter and sort after it. And if i add a "normal" value it places before it. and that works too - like this:
?fss=yellow+cat5&sps=d211av450240_2.0
The problem is that when I have several of the new custom parameter in a group. In the other checkbox groups I can only have one value - and the chosen one replaces the old parameter. with the custom parameter I want to add it just ads on, instead of replaceing - like this.
?fss=yellow+cat5&sps=d211av450240_2.0+&sps=d211av450240_1.0
The correct outcome would have been
?fss=yellow+cat5&sps=d211av450240_1.0
I know this is because of the "&" in the parameter - but unsure how to work it out. I cant change the parameter either - and I know checkboxes are ment to be for multiple choices, but its like this for a reason.
My filterscript:
$(document).ready(function(){
new function(settings) {
var $separator = settings.separator || '&';
var $spaces = settings.spaces === false ? false : true;
var $suffix = settings.suffix === false ? '' : '[]';
var $prefix = settings.prefix === false ? false : true;
var $hash = $prefix ? settings.hash === true ? "#" : "?" : "";
var $numbers = settings.numbers === false ? false : true;
jQuery.query = new function() {
var is = function(o, t) {
return o != undefined && o !== null && (!!t ? o.constructor == t : true);
};
var parse = function(path) {
var m, rx = /\[([^[]*)\]/g, match = /^([^[]+)(\[.*\])?$/.exec(path), base = match[1], tokens = [];
while (m = rx.exec(match[2])) tokens.push(m[1]);
return [base, tokens];
};
var set = function(target, tokens, value) {
var o, token = tokens.shift();
if (typeof target != 'object') target = null;
if (token === "") {
if (!target) target = [];
if (is(target, Array)) {
target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
} else if (is(target, Object)) {
var i = 0;
while (target[i++] != null);
target[--i] = tokens.length == 0 ? value : set(target[i], tokens.slice(0), value);
} else {
target = [];
target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
}
} else if (token && token.match(/^\s*[0-9]+\s*$/)) {
var index = parseInt(token, 10);
if (!target) target = [];
target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
} else if (token) {
var index = token.replace(/^\s*|\s*$/g, "");
if (!target) target = {};
if (is(target, Array)) {
var temp = {};
for (var i = 0; i < target.length; ++i) {
temp[i] = target[i];
}
target = temp;
}
target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
} else {
return value;
}
return target;
};
var queryObject = function(a) {
var self = this;
self.keys = {};
if (a.queryObject) {
jQuery.each(a.get(), function(key, val) {
self.SET(key, val);
});
} else {
self.parseNew.apply(self, arguments);
}
return self;
};
queryObject.prototype = {
queryObject: true,
parseNew: function(){
var self = this;
self.keys = {};
jQuery.each(arguments, function() {
var q = "" + this;
q = q.replace(/^[?#]/,''); // remove any leading ? || #
q = q.replace(/[;&]$/,''); // remove any trailing & || ;
if ($spaces) q = q.replace(/[+]/g,' '); // replace +'s with spaces
jQuery.each(q.split(/[&;]/), function(){
var key = decodeURIComponent(this.split('=')[0] || "");
var val = decodeURIComponent(this.split('=')[1] || "");
if (!key) return;
if ($numbers) {
if (/^[+-]?[0-9]+\.[0-9]*$/.test(val)) // simple float regex
val = parseFloat(val);
else if (/^[+-]?[0-9]+$/.test(val)) // simple int regex
val = parseInt(val, 10);
}
val = (!val && val !== 0) ? true : val;
self.SET(key, val);
});
});
return self;
},
has: function(key, type) {
var value = this.get(key);
return is(value, type);
},
GET: function(key) {
if (!is(key)) return this.keys;
var parsed = parse(key), base = parsed[0], tokens = parsed[1];
var target = this.keys[base];
while (target != null && tokens.length != 0) {
target = target[tokens.shift()];
}
return typeof target == 'number' ? target : target || "";
},
get: function(key) {
var target = this.GET(key);
if (is(target, Object))
return jQuery.extend(true, {}, target);
else if (is(target, Array))
return target.slice(0);
return target;
},
SET: function(key, val) {
var value = !is(val) ? null : val;
var parsed = parse(key), base = parsed[0], tokens = parsed[1];
var target = this.keys[base];
this.keys[base] = set(target, tokens.slice(0), value);
return this;
},
set: function(key, val) {
return this.copy().SET(key, val);
},
REMOVE: function(key) {
return this.SET(key, null).COMPACT();
},
remove: function(key) {
return this.copy().REMOVE(key);
},
EMPTY: function() {
var self = this;
jQuery.each(self.keys, function(key, value) {
delete self.keys[key];
});
return self;
},
load: function(url) {
var hash = url.replace(/^.*?[#](.+?)(?:\?.+)?$/, "$1");
var search = url.replace(/^.*?[?](.+?)(?:#.+)?$/, "$1");
return new queryObject(url.length == search.length ? '' : search, url.length == hash.length ? '' : hash);
},
empty: function() {
return this.copy().EMPTY();
},
copy: function() {
return new queryObject(this);
},
COMPACT: function() {
function build(orig) {
var obj = typeof orig == "object" ? is(orig, Array) ? [] : {} : orig;
if (typeof orig == 'object') {
function add(o, key, value) {
if (is(o, Array))
o.push(value);
else
o[key] = value;
}
jQuery.each(orig, function(key, value) {
if (!is(value)) return true;
add(obj, key, build(value));
});
}
return obj;
}
this.keys = build(this.keys);
return this;
},
compact: function() {
return this.copy().COMPACT();
},
toString: function() {
var i = 0, queryString = [], chunks = [], self = this;
var encode = function(str) {
str = str + "";
if ($spaces) str = str.replace(/ /g, "+");
return encodeURIComponent(str);
};
var addFields = function(arr, key, value) {
if (!is(value) || value === false) return;
var o = [encode(key)];
if (value !== true) {
o.push("=");
o.push(encode(value));
}
arr.push(o.join(""));
};
var build = function(obj, base) {
var newKey = function(key) {
return !base || base == "" ? [key].join("") : [base, "[", key, "]"].join("");
};
jQuery.each(obj, function(key, value) {
if (typeof value == 'object')
build(value, newKey(key));
else
addFields(chunks, newKey(key), value);
});
};
build(this.keys);
if (chunks.length > 0) queryString.push($hash);
queryString.push(chunks.join($separator));
return queryString.join("");
}
};
return new queryObject(location.search, location.hash);
};
}(jQuery.query || {}); // Pass in jQuery.query as settings object
function removeFSS() {
ga("send", "event", "button", "click", "filter-clear", "input");
var t = encodeURI(unescape($.query.set("fss", '')));
var n = window.location.href.split("?")[0]; window.location.href = n + t
}
function getFSS() {
var D = jQuery('.filterGenius input, .filterGenius select').serializeArray();
var O = {};
jQuery.each(D, function(_, kv) {
if (O.hasOwnProperty(kv.name)) {
O[kv.name] = jQuery.makeArray(O[kv.name]);
O[kv.name].push(clean(kv.value, ""));
}
else {
O[kv.name] =kv.value;
}
});
var V = [];
for(var i in O)
if(jQuery.isArray(O[i]))
V.push(O[i].join("+"));
else
V.push(O[i]);
V = jQuery.grep(V,function(n){ return(n) });
return V.join("+");
}
$(document).ready(function () {
$(".filterGenius input").each(function () {
if (window.location.href.indexOf($(this).val()) >= 0) {
$(this).attr("checked", "checked")
}
});
$(".filterGenius select option").each(function () {
if (window.location.href.indexOf($(this).val()) >= 0) {
$(this).attr('selected', true);
}
});
$(".filterGenius input, .filterGenius select").change(function () {
var s = encodeURI(unescape(jQuery.query.set("fss", getFSS())));
var o = window.location.href.split("?")[0];
$(".filterGenius input, .filterGenius select").attr("disabled", true);
window.location.href = o + s
});
});
$('input[type="checkbox"]').on('change', function() {
$('input[name="' + this.name + '"]').not(this).prop('checked', false);
});
});
I am using tableToJson, which you may or may not be familiar with, to convert a table to JSON (Ive included the code for it here if you don't know it). Simple enough. The output is entirely as expected and looks like:
{"data":[
{"Time":"6:30am - 8:30am","Monday":"","Tuesday":"Maths","Wednesday":"","Thursday":"","Friday":""},
{"Time":"11:15am - 12:45pm","Monday":"Maths","Tuesday":"","Wednesday":"English","Thursday":"","Friday":""},
{"Time":"3:00pm - 5:00pm","Monday":"","Tuesday":"","Wednesday":"English","Thursday":"","Friday":""},
{"Time":"5:00pm - 7:00pm","Monday":"","Tuesday":"Science","Wednesday":"","Thursday":"","Friday":""},
{"Time":"7:00pm - 9:00pm","Monday":"","Tuesday":"Science","Wednesday":"","Thursday":"","Friday":""}]
}
However, I need to expand on it to take the #ids of each <tr> so the JSON looks like: (Assuming the table HTML is <tr id="session 1"></tr> etc.)
{
"data":{
"session 1":{"Time":"6:30am - 8:30am","Monday":"","Tuesday":"sdfsdf","Wednesday":"","Thursday":"","Friday":""},
"session 2":{"Time":"11:15am - 12:45pm","Monday":"sdfsdfsdf","Tuesday":"","Wednesday":"","Thursday":"","Friday":""},
"session 3":{"Time":"3:00pm - 5:00pm","Monday":"","Tuesday":"","Wednesday":"","Thursday":"","Friday":""},
"session 4":{"Time":"5:00pm - 7:00pm","Monday":"","Tuesday":"","Wednesday":"","Thursday":"","Friday":""},
"session 5":{"Time":"7:00pm - 9:00pm","Monday":"","Tuesday":"","Wednesday":"","Thursday":"","Friday":""}
}
}
For reference The code for tableToJson looks like:
(function( $ ) {
'use strict';
$.fn.tableToJSON = function(opts) {
// Set options
var defaults = {
ignoreColumns: [],
onlyColumns: null,
ignoreHiddenRows: true,
ignoreEmptyRows: false,
headings: null,
allowHTML: false,
includeRowId: false,
textDataOverride: 'data-override',
textExtractor: null
};
opts = $.extend(defaults, opts);
var notNull = function(value) {
return value !== undefined && value !== null;
};
var ignoredColumn = function(index) {
if( notNull(opts.onlyColumns) ) {
return $.inArray(index, opts.onlyColumns) === -1;
}
return $.inArray(index, opts.ignoreColumns) !== -1;
};
var arraysToHash = function(keys, values) {
var result = {},index=0, name = 'test';
$.each(values, function(i, value) {
// when ignoring columns, the header option still starts
// with the first defined column
if ( index < keys.length && notNull(value) ) {
result[ keys[index] ] = value;
index++;
}
});
return result;
};
var cellValues = function(cellIndex, cell, isHeader) {
var $cell = $(cell),
// textExtractor
extractor = opts.textExtractor,
override = $cell.attr(opts.textDataOverride);
// don't use extractor for header cells
if ( extractor === null || isHeader ) {
return $.trim( override || ( opts.allowHTML ? $cell.html() : cell.textContent || $cell.text() ) || '' );
} else {
// overall extractor function
if ( $.isFunction(extractor) ) {
return $.trim( override || extractor(cellIndex, $cell) );
} else if ( typeof extractor === 'object' && $.isFunction( extractor[cellIndex] ) ) {
return $.trim( override || extractor[cellIndex](cellIndex, $cell) );
}
}
// fallback
return $.trim( override || ( opts.allowHTML ? $cell.html() : cell.textContent || $cell.text() ) || '' );
};
var rowValues = function(row, isHeader) {
var result = [];
var includeRowId = opts.includeRowId;
var useRowId = (typeof includeRowId === 'boolean') ? includeRowId : (typeof includeRowId === 'string') ? true : false;
var rowIdName = (typeof includeRowId === 'string') === true ? includeRowId : 'rowId';
if (useRowId) {
if (typeof $(row).attr('id') === 'undefined') {
result.push(rowIdName);
}
}
$(row).children('td,th').each(function(cellIndex, cell) {
result.push( cellValues(cellIndex, cell, isHeader) );
});
return result;
};
var getHeadings = function(table) {
var firstRow = table.find('tr:first').first();
return notNull(opts.headings) ? opts.headings : rowValues(firstRow, true);
};
var construct = function(table, headings) {
var i, j, len, len2, txt, $row, $cell,
tmpArray = [], cellIndex = 0, result = [];
table.children('tbody,*').children('tr').each(function(rowIndex, row) {
if( rowIndex > 0 || notNull(opts.headings) ) {
var includeRowId = opts.includeRowId;
var useRowId = (typeof includeRowId === 'boolean') ? includeRowId : (typeof includeRowId === 'string') ? true : false;
$row = $(row);
var isEmpty = ($row.find('td').length === $row.find('td:empty').length) ? true : false;
if( ( $row.is(':visible') || !opts.ignoreHiddenRows ) && ( !isEmpty || !opts.ignoreEmptyRows ) && ( !$row.data('ignore') || $row.data('ignore') === 'false' ) ) {
cellIndex = 0;
if (!tmpArray[rowIndex]) {
tmpArray[rowIndex] = [];
}
if (useRowId) {
cellIndex = cellIndex + 1;
if (typeof $row.attr('id') !== 'undefined') {
tmpArray[rowIndex].push($row.attr('id'));
} else {
tmpArray[rowIndex].push('');
}
}
$row.children().each(function(){
$cell = $(this);
// skip column if already defined
while (tmpArray[rowIndex][cellIndex]) { cellIndex++; }
// process rowspans
if ($cell.filter('[rowspan]').length) {
len = parseInt( $cell.attr('rowspan'), 10) - 1;
txt = cellValues(cellIndex, $cell);
for (i = 1; i <= len; i++) {
if (!tmpArray[rowIndex + i]) { tmpArray[rowIndex + i] = []; }
tmpArray[rowIndex + i][cellIndex] = txt;
}
}
// process colspans
if ($cell.filter('[colspan]').length) {
len = parseInt( $cell.attr('colspan'), 10) - 1;
txt = cellValues(cellIndex, $cell);
for (i = 1; i <= len; i++) {
// cell has both col and row spans
if ($cell.filter('[rowspan]').length) {
len2 = parseInt( $cell.attr('rowspan'), 10);
for (j = 0; j < len2; j++) {
tmpArray[rowIndex + j][cellIndex + i] = txt;
}
} else {
tmpArray[rowIndex][cellIndex + i] = txt;
}
}
}
txt = tmpArray[rowIndex][cellIndex] || cellValues(cellIndex, $cell);
if (notNull(txt)) {
tmpArray[rowIndex][cellIndex] = txt;
}
cellIndex++;
});
}
}
});
$.each(tmpArray, function( i, row ){
if (notNull(row)) {
// remove ignoredColumns / add onlyColumns
var newRow = notNull(opts.onlyColumns) || opts.ignoreColumns.length ?
$.grep(row, function(v, index){ return !ignoredColumn(index); }) : row,
// remove ignoredColumns / add onlyColumns if headings is not defined
newHeadings = notNull(opts.headings) ? headings :
$.grep(headings, function(v, index){ return !ignoredColumn(index); });
txt = arraysToHash(newHeadings, newRow);
result[result.length] = txt;
}
});
return result;
};
// Run
var headings = getHeadings(this);
return construct(this, headings);
};
})( jQuery );
I'm probably missing something really simple, but honestly cannot see how to do this. Any help would be super appreciated. Thank you all in advance!
Please rethink why you'd need to change it. This already provides valid ordered content in a JSON array (accessible via foo.data[0], foo.data[1] etc, or iterable with for(var i; i<foo.data.length;i++){}).
What you want will give you something with no guaranteed order (properties order in an object is irrelevant in JSON) and goes against the flow).
Edit: Stupid API.
If you must conform to some obscure and evil standard, edit the data after it was created. You can't expect $.fn.tableToJSON not to conform to JSON & javascript standards...
var foo = // output from $.fn.tableToJSON
var out = {data:{}}; //declare an *object* instead of an array
for(var i = 0; i < foo.data.length; i++){
out.data['Session ' + (i + 1)] = foo.data[i];
}
//debug output
console.log(JSON.stringify(out))
Someone mentioned I should use a true Factory pattern below so I don't have to constantly supply the typeName. How can this be accomplished within JavaScript and Angular. If it were C#, I wouldn't have a problem, but the Java reference / value types and Angular are making my brain hurt.
(function () {
'use strict';
angular
.module('blocks.object-cache');
objectCache.$inject = ['CacheFactory', '$auth'];
function objectCache(CacheFactory, $auth) {
var _options = {
maxAge : (60 * 60 * 1000),
deleteOnExpire : 'passive',
storageMode : 'localStorage'
};
var service = {
setOptions : setOptions,
getCache : getCache,
clear : clear,
getAll : getAll,
getItem : getItem,
getItems : getItems,
putItem : putItem,
putItems : putItems,
getItemsByKey : getItemsByKey,
getItemByKeyFirst : getItemByKeyFirst,
getItemByKeySingle : getItemByKeySingle,
removeItemsByKey : removeItemsByKey,
removeItemByKey : removeItemByKey,
putItemsByKey : putItemsByKey,
putItemByKey : putItemByKey
};
return service;
////////////////////////////////////////////////////////////////////////////////
function setOptions (options) {
options = options || {};
options.maxAge = options.maxAge = _options.maxAge;
options.deleteOnExpire = options.deleteOnExpire = _options.deleteOnExpire;
options.storageMode = options.storageMode = _options.storageMode;
_options = options;
}
function getCache(typeName) {
var cacheName = [getUserId(), normalizeTypeName(typeName || 'objects')].join('_');
var cache = CacheFactory(cacheName);
if (cache) { return cache; }
cache = CacheFactory(cacheName, _options);
return cache;
}
function clear (typeName) {
var cache = getCache(typeName);
cache.removeAll();
return (!cache.keys() || (cache.keys().length < 1));
}
function getAll (typeName) {
var cache = getCache(typeName);
var result = [];
(cache.keys() || []).forEach(function(key){
result.push(cache(key));
});
return result;
}
function getItem(typeName, id) {
if (typeof id == 'undefined' || !id.trim) { return null; }
var cache = getCache(typeName);
return cache.get(id);
}
function getItems(typeName, ids) {
var cache = getCache(typeName),
result = [],
_ids = [];
(ids || []).forEach(function(id){
if (_ids.indexOf(id) < 0) {
_ids.push(id);
var item = cache.get(id);
if (item) { result.push(item); }
}
});
return result;
}
function putItem(typeName, item, id, refresh) {
if (typeof item == 'undefined') { return false; }
if (typeof id == 'undefined' || !id.trim) { return false; }
var cache = getCache(typeName);
var existing = cache.get(id);
if (existing && !refresh) { return true; }
if (existing) { cache.remove(id); }
cache.put(item, id);
return (!!cache.get(id));
}
function putItems(typeName, items, idField, refresh) {
var cache = getCache(typeName);
(items || []).forEach(function(item){
var id = item[idField];
if (typeof id != 'undefined') {
var existing = cache.get(id);
if (existing && !!refresh) { cache.remove(id); }
if (!existing || !!refresh) { cache.put(item, id); }
if (!cache.get(id)) { return false; }
}
});
return true;
}
function getItemsByKey(typeName, key, value, isCaseSensitive) {
var result = [];
(getAll(typeName) || []).forEach(function(item){
var itemValue = item[key];
if (typeof itemValue != 'undefined') {
if ((typeof value == 'string') && (typeof itemValue == 'string') && (!isCaseSensitive || value.toLowerCase() == itemValue.toLowerCase())) {
result.push(item);
} else if (((typeof value) == (typeof itemValue)) && (value == itemValue)) {
result.push(item);
} else {
// Other scenarios?
}
}
});
return result;
}
function getItemByKeyFirst(typeName, key, value, isCaseSensitive) {
var items = getItemsByKey(typeName, key, value, isCaseSensitive) || [];
return (items.length > 0) ? items[0] : null;
}
function getItemByKeySingle(typeName, key, value, isCaseSensitive) {
var items = getItemsByKey(typeName, key, value, isCaseSensitive) || [];
return (items.length === 0) ? items[0] : null;
}
function removeItemsByKey (typeName, keyField, values, isCaseSensitive) {
var cache = getCache(typeName),
keysToRemove = [];
(cache.keys() || []).forEach(function(key){
var item = cache.get[key],
itemValue = item[keyField];
if (typeof itemValue != 'undefined') {
for (var v = 0; v < (values || []).length; v += 1) {
if ((typeof values[v] == 'string') && (typeof itemValue == 'string') && (!isCaseSensitive || values[v].toLowerCase() == itemValue.toLowerCase())) {
keysToRemove.push(key);
break;
} else if (((typeof values[v]) == (typeof itemValue)) && (values[v] == itemValue)) {
keysToRemove.push(key);
break;
} else {
// Other scenarios?
}
}
}
});
var success = true;
keysToRemove.forEach(function(key){
cache.remove(key);
if (cache.get(key)) { success = false; }
});
return success;
}
function removeItemByKey (typeName, keyField, value, isCaseSensitive) {
return removeItemsByKey(typeName, keyField, [value], isCaseSensitive);
}
function putItemsByKey(typeName, items, keyField, refresh, isCaseSensitive) {
if (!!refresh) {
var values = _.map((items || []), keyField);
if (!removeItemsByKey(typeName, keyField, values, isCaseSensitive)) { return false; }
}
var cache = getCache(typeName);
(items || []).forEach(function(item){
var id = item[keyField];
if (typeof value != 'undefined') { cache.put(item, id); }
if (!cache.get(id)) { return false; }
});
return true;
}
function putItemByKey(typeName, item, keyField, refresh, isCaseSensitive) {
return putItemsByKey(typeName, [item], keyField, refresh, isCaseSensitive);
}
function getUserId () {
return $auth.isAuthenticated() ? ($auth.getPayload().sub || 'unknown') : 'public';
}
function normalizeTypeName (typeName) {
return typeName.split('.').join('-');
}
}
})();
I'm not an Angular guru, but couldn't you just move the functions to achieve a factory-like pattern? I have not tested this, but with about two minutes of copy-n-paste...
Edit: Removed your nested iterate functions.
(function () {
'use strict';
angular
.module('blocks.object-cache')
.service('ObjectCache', ObjectCache);
ObjectCache.$inject = ['CacheFactory', '$auth'];
function ObjectCache(CacheFactory, $auth) {
var _options = {
maxAge : (60 * 60 * 1000),
deleteOnExpire : 'passive',
storageMode : 'localStorage'
};
var factory = {
getCache : getCache
};
return factory;
////////////////////////////
function getCache(typeName, options) {
options = options || {};
options.maxAge = options.maxAge = _options.maxAge;
options.deleteOnExpire = options.deleteOnExpire = _options.deleteOnExpire;
options.storageMode = options.storageMode = _options.storageMode;
typeName = normalizeTypeName(typeName || 'objects');
var userId = getUserId() || 'public';
var name = userId + '_' + typeName;
var service = {
type : typeName,
user : userId,
name : name,
options : options,
cache : CacheFactory(name) || CacheFactory.createCache(name, options),
clear : function () {
this.cache.removeAll();
},
getAll : function () {
var result = [];
var keys = this.cache.keys() || [];
for (var i = 0; i < keys.length; i += 1) {
result.push(this.cache(keys[i]));
}
return result;
},
getItems : function (ids) {
var result = [],
_ids = [];
for (var i = 0; i < (ids || []).length; i += 1) {
var id = ids[i];
if (_ids.indexOf(id) < 0) {
_ids.push(id);
var item = this.cache.get(id);
if (item) { result.push(item); }
}
}
return result;
},
getItem : function (id) {
var items = this.getItems([id]);
return (items.length > 0) ? items[0] : null;
},
putItem : function (item, id, refresh) {
var existing = this.cache.get(id);
if (existing && !refresh) { return true; }
if (existing) { this.cache.remove(id); }
this.cache.put(item, id);
return (!!this.cache.get(id));
},
putItems : function (items, idField, refresh) {
var success = true;
for (var i = 0; i < (items || []).length; i += 1) {
var item = items[i];
var id = item[idField];
if (typeof id != 'undefined') {
if (this.putItem(item, id, refresh)) { success = false; }
}
}
return success;
},
getItemsByKey : function (key, value, isCaseSensitive) {
var result = [];
(this.getAll() || []).forEach(function(item){
var itemValue = item[key];
if (typeof itemValue != 'undefined') {
if ((typeof value == 'string') && (typeof itemValue == 'string') && (!isCaseSensitive || value.toLowerCase() == itemValue.toLowerCase())) {
result.push(item);
} else if (((typeof value) == (typeof itemValue)) && (value == itemValue)) {
result.push(item);
} else {
// Other scenarios?
}
}
});
return result;
},
getItemByKeyFirst : function (key, value, isCaseSensitive) {
var items = this.getItemsByKey(key, value, isCaseSensitive) || [];
return (items.length > 0) ? items[0] : null;
},
getItemByKeySingle : function (key, value, isCaseSensitive) {
var items = this.getItemsByKey(key, value, isCaseSensitive) || [];
return (items.length === 0) ? items[0] : null;
},
removeItemsByKey : function (keyField, values, isCaseSensitive) {
var keysToRemove = [],
keys = this.cache.keys() || [];
for (var k = 0; k < keys.length; k += 1) {
var key = keys[k];
var item = this.cache.get(key);
var itemVal = item[keyField];
if (typeof itemVal != 'undefined') {
for (var v = 0; v < (values || []).length; v += 1) {
if ((typeof values[v] == 'string') && (typeof itemVal == 'string') && (!isCaseSensitive || values[v].toLowerCase() == itemVal.toLowerCase())) {
keysToRemove.push(key);
break;
} else if (((typeof values[v]) == (typeof itemVal)) && (values[v] == itemVal)) {
keysToRemove.push(key);
break;
} else {
// Other scenarios?
}
}
}
}
var success = true;
for (var r = 0; r < keysToRemove.length; r += 1) {
this.cache.remove(keysToRemove[r]);
if (this.cache.get(keysToRemove[r])) { success = false; }
}
return success;
},
removeItemByKey : function (keyField, value, isCaseSensitive) {
return this.removeItemsByKey(keyField, [value], isCaseSensitive);
},
putItemsByKey : function(items, keyField, refresh, isCaseSensitive) {
if (!!refresh) {
var values = _.map((items || []), keyField);
if (!this.removeItemsByKey(keyField, values, isCaseSensitive)) { return false; }
}
for (var i = 0; i < (items || []).length; i += 1) {
var id = items[i][keyField];
if (typeof id != 'undefined') {
this.cache.put(items[i], id);
if (!this.cache.get(id)) { return false; }
}
}
return true;
},
putItemByKey : function (item, keyField, refresh, isCaseSensitive) {
return this.putItemsByKey([item], keyField, refresh, isCaseSensitive);
}
};
return service;
}
function getUserId () {
return $auth.isAuthenticated() ? ($auth.getPayload().sub || 'unknown') : null;
}
function normalizeTypeName (typeName) {
return typeName.split('.').join('-');
}
}
})();