md-utils/index.js

100 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-03-23 15:52:57 +00:00
'use strict';
/**
*
* User: Martin Donnelly
* Date: 2016-03-15
* Time: 15:08
*
*/
module.exports = {
2016-03-30 13:24:22 +00:00
newObjectFrom(source, fields) {
var rObj = {};
for (var item in fields) {
if (source.hasOwnProperty(fields[item])) {
rObj[fields[item]] = source[fields[item]];
}
}
return rObj;
}, reDashObject(source) {
var rObj = {};
for (var item in source) {
if (source.hasOwnProperty(item)) {
let newName = item.replace('_', '-');
rObj[newName] = source[item];
2016-03-23 15:52:57 +00:00
}
2016-03-30 13:24:22 +00:00
}
2016-03-23 15:52:57 +00:00
2016-03-30 13:24:22 +00:00
return rObj;
}, unDashObject(source) {
2016-03-23 15:52:57 +00:00
var rObj = {};
for (var item in source) {
if (source.hasOwnProperty(item)) {
let newName = item.replace('-', '_');
rObj[newName] = source[item];
}
}
return rObj;
}, populateObject(source, reform) {
var rObj = {};
for (var item in reform) {
if (source.hasOwnProperty()) {
rObj[item] = source[reform[item].from];
}
}
return rObj;
}, sanitiseObj: function(obj, methods) {
for (var item in methods) {
if (obj.hasOwnProperty(item)) {
if (typeof obj[item] === 'string') {
obj[item] = this.strip(obj[item], methods[item]);
}
}
}
return obj;
}, strip: function(text, loose) {
var _loose = loose || false;
var ch;
var s = [];
var SCRIPT_REGEX = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
var CHECKCHARS = "0123456789" + // Numeric
2016-03-30 13:24:22 +00:00
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
"abcdefghijklmnopqrstuvwxyz-'" + (_loose === false ? '' : "_,.!~*()@:+/\\");
2016-03-23 15:52:57 +00:00
if (typeof text === 'string') {
while (SCRIPT_REGEX.test(text)) {
text = text.replace(SCRIPT_REGEX, '');
}
for (var i = 0; i < text.length; i++) {
ch = text.charAt(i);
if (ch === ' ') {
s.push(' ');
2016-03-30 13:24:22 +00:00
}
else if (ch.charCodeAt(0) < 255 && CHECKCHARS.indexOf(ch) !== -1) {
2016-03-23 15:52:57 +00:00
s.push(ch);
}
}
text = s.join('').trim();
}
return text;
}, cloneTrim: function(from) {
var _out = {};
if (typeof from === 'object' && Object.keys(from).length !== 0) {
// copy the required fields into the data object..
for (var key in from) {
if (from.hasOwnProperty(key)) {
_out[key] = typeof from[key] === 'string' ? from[key].trim() : from[key];
}
}
}
return _out;
}
};