Skip to content

Instantly share code, notes, and snippets.

@akhoury
Last active January 3, 2016 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhoury/8529214 to your computer and use it in GitHub Desktop.
Save akhoury/8529214 to your computer and use it in GitHub Desktop.
- PreNodeBB-PullRequest preview: - Affected files: public/src/utils.js, src/meta.js - Purpose: Allowing the 'config' hash to safely save Stringified Objects and Arrays, but leave everything else alone.
// var definitions for demo-gist only
// there is also a mock util object below
var Meta = {};
var Meta.configs = {
// .. other Meta.configs functions, such as set, get, getFields etc...
// obviously the function names aren't the best, but I didn't want to override existing ones
// sets a config field in the DB, as a stringified JSON if it's an Object or an Array,
// if its xType, @see util.xTypeOf(), is defined in the 'options.skip' hash, it's saved as it is,
// @see util.xStringify()
_setField: function(field, value, callback) {
value = utils.xStringify(
value, {
skip: {
'string': 1,
'number': 1,
'null': 1,
'undefined': 1,
'date': 1,
'regexp': 1
}
}
);
db.setObjectField('config', field, value, callback);
},
// return an object of config fields, with appropriate JSON.parsing when the results looks like an Object or Array
// if its xType, @see util.xTypeOf(), is defined in the 'options.skip', it's returned as it is
// @see util.xParse()
_getFields: function(fields, callback) {
db.getObjectFields('config', fields, function(err, returnData){
if (!err) {
Object.keys(returnData).forEach(function(key){
returnData[key] = utils.xParse(
returnData[key], {
failTo: 'original',
skip: {
'number': 1,
'string': 1,
'null': 1,
'undefined': 1,
'date': 1,
'error': 1,
'regexp': 1
}});
});
}
if (typeof callback == 'function')
callback(err, returnData);
});
},
// get a single field, as Object or an Array if it looks like one
// uses _getFields()
_getField: function(field, callback) {
Meta.configs._getFields([field], callback);
}
};
// var definition for demo-gist only.
var util = {
// actually this would go into util.js
// ... other util functions
// return real typeof, such as boolean, string, number, array, object, function, undefined, null, date, error, regexp, math, json, arguments
// todo: typeOf(NaN) still returns 'number', should return 'nan' maybe?
xTypeOf: (function() {
var re = /\s([a-zA-Z]+)/;
return function(obj){
return Object.prototype.toString.call(obj).match(re)[1].toLowerCase();
};
})(),
// @requires xTypeOf
xStringify: function(value, options) {
options = options || {};
options.skip = options.skip || {};
var result = null, xType = this.xTypeOf(value);
if (options.skip === true || options.skip[xType])
return value;
try {
result = JSON.stringify(value, options.replacer, options.space);
} catch (e) {
result = '';
}
return result;
},
// @requires xTypeOf
xParse: function(value, options) {
options = options || {};
options.failTo = options.failTo || '';
var result = null, xType;
if (options.skip === true) {
return value;
} else {
options.skip = options.skip || {};
}
try {
result = JSON.parse(value);
} catch (e) {
result = options.failTo == 'original' ? value : options.failTo == 'object' ? {} : result ;
}
xType = this.xTypeOf(result);
if (options.skip(xType)) {
return value;
} else {
return result;
}
},
/// ... more util functions
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment