Socket
Socket
Sign inDemoInstall

protoblast

Package Overview
Dependencies
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

protoblast - npm Package Compare versions

Comparing version 0.5.6 to 0.5.7

lib/rurl.js

10

CHANGELOG.md

@@ -0,1 +1,11 @@

## 0.5.7 (2018-07-01)
* Add `Date.parseDuration(str)` for getting a duration in ms
* `Date#add` and `Date#subtract` now also accept duration strings
* Total `RURL` rewrite, inspired by `url-parse` by Arnout Kazemier and `URI.js` by Rodney Rehm
* `Object.flatten(obj, divider)` now accepts custom dividers
* `RURL.encodeQuery(obj)` now handles nested objects
* `String#assign(values, remove_used)` attempts to normalize values when assigning objects
* Add `Date.parseString(str, base)` and `Date.parseStringToTime(str, base)`
## 0.5.6 (2018-06-20)

@@ -2,0 +12,0 @@

118

lib/browsershims.js

@@ -74,120 +74,2 @@ module.exports = function BlastBrowserShims(Blast, Collection, Bound) {

/**
* Serialize an object to a query string
*
* @author Joyent, Inc. and other Node contributors
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.1.3
* @version 0.4.1
*
* @param {Object} obj
* @param {String} sep
* @param {String} eq
* @param {Object} options
*
* @return {String}
*/
Blast.defineStatic('URL', 'encodeQuery', function encodeQuery(obj, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
// Use our custom method, so brackets don't get encoded
var encode = Collection.URL.encodeUriSegment;
if (options && typeof options.encodeURIComponent === 'function') {
encode = options.encodeURIComponent;
}
if (obj && typeof obj === 'object') {
var keys = Object.keys(obj);
var fields = [];
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
var v = obj[k];
var ks = encode(stringifyPrimitive(k)) + eq;
if (Array.isArray(v)) {
for (var j = 0; j < v.length; j++)
fields.push(ks + encode(stringifyPrimitive(v[j])));
} else {
fields.push(ks + encode(stringifyPrimitive(v)));
}
}
return fields.join(sep);
}
return '';
});
/**
* Parse a query string to an object
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.1.3
* @version 0.1.3
*
* @param {String} str
* @param {String} sep
* @param {String} eq
* @param {Object} options
*
* @return {Object}
*/
Blast.defineStatic('URL', 'parseQuery', function parseQuery(qs, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
var obj = {};
if (typeof qs !== 'string' || qs.length === 0) {
return obj;
}
var regexp = /\+/g;
qs = qs.split(sep);
var maxKeys = 1000;
if (options && typeof options.maxKeys === 'number') {
maxKeys = options.maxKeys;
}
var len = qs.length;
// maxKeys <= 0 means that we should not limit keys count
if (maxKeys > 0 && len > maxKeys) {
len = maxKeys;
}
var decode = decodeURIComponent;
if (options && typeof options.decodeURIComponent === 'function') {
decode = options.decodeURIComponent;
}
for (var i = 0; i < len; ++i) {
var x = qs[i].replace(regexp, '%20'),
idx = x.indexOf(eq),
kstr, vstr, k, v;
if (idx >= 0) {
kstr = x.substr(0, idx);
vstr = x.substr(idx + 1);
} else {
kstr = x;
vstr = '';
}
k = decode(kstr);
v = decode(vstr);
if (!hasOwnProperty(obj, k)) {
obj[k] = v;
} else if (Array.isArray(obj[k])) {
obj[k].push(v);
} else {
obj[k] = [obj[k], v];
}
}
return obj;
});
/**
* Parse an HTML string

@@ -194,0 +76,0 @@ *

398

lib/date.js
module.exports = function BlastDate(Blast, Collection, Bound, Obj) {
var unitMillisecondFactors,
msUnits;
var rx_duration = /[-+]|(this|next|last|previous)\s+(\w+)|((-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*))|\w+/ig,
ms_units,
days,
key;
msUnits = unitMillisecondFactors = {
'm' : 1,
ms_units = {
'n' : 1 / 1e6,
'nanosecond' : 1 / 1e6,
'µs' : 1 / 1e3,
'microsecond' : 1 / 1e3,
'ms' : 1,
'millisecond' : 1,
's' : 1e3,
'sec' : 1e3,
'second' : 1e3,
'm' : 6e4,
'min' : 6e4,
'minute' : 6e4,
'h' : 36e5,
'hr' : 36e5,
'hour' : 36e5,
'd' : 864e5,
'day' : 864e5,
'w' : 6048e5,
'wk' : 6048e5,
'week' : 6048e5,
'isoweek' : 6048e5,
'b' : 2592e6,
'month' : 2592e6,
'y' : 31536e6,
'yr' : 31536e6,
'year' : 31536e6
};
days = {
sun : 0,
mon : 1,
tue : 2,
tues : 2,
wed : 3,
wednes : 3,
thu : 4,
thurs : 4,
fri : 5,
sat : 6,
satur : 6
};
// Add plurals to the object
for (key in ms_units) {
if (key.length > 1 && key[key.length - 1] != 's') {
ms_units[key + 's'] = ms_units[key];
}
}
// Add 'day' to the days
for (key in days) {
days[key + 'day'] = days[key];
}
/**

@@ -46,2 +90,24 @@ * Create a new date object

/**
* Get a specific unit's duration in ms, or 0 if it doesn't exist
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.5.7
* @version 0.5.7
*
* @param {String} unit
*
* @return {Number}
*/
Blast.defineStatic('Date', 'getUnitMs', function getUnitMs(unit) {
var result = ms_units[unit];
if (!result) {
result = ms_units[unit.toLowerCase()];
}
return result || 0;
});
/**
* Determine the difference between two dates.

@@ -92,3 +158,3 @@ *

if (unit != null) {
diff /= msUnits[unit];
diff /= ms_units[unit];
}

@@ -99,3 +165,147 @@

var date_tokens = {
today: function(date) {
return Bound.Date.startOf(date, 'day');
},
tomorrow: function(date) {
return Bound.Date.add(this.today(date), 1, 'day');
},
yesterday: function(date) {
return Bound.Date.subtract(this.today(date), 1, 'day');
}
};
/**
* Parse datetime strings and return a timestamp
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.5.7
* @version 0.5.7
*
* @param {String} str The input string
* @param {String} base The time basis [0]
*
* @return {Date}
*/
Blast.defineStatic('Date', 'parseString', function parseString(str, base) {
return new Date(Blast.Bound.Date.parseStringToTime(str, base));
});
/**
* Parse datetime strings and return a timestamp
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.5.7
* @version 0.5.7
*
* @param {String} str The input string
* @param {String} base The time basis [0]
*
* @return {Number}
*/
Blast.defineStatic('Date', 'parseStringToTime', function parseStringToTime(str, base) {
var unit_time,
base_date,
result,
entry,
type,
name,
unit,
val,
add,
d;
if (base == null) {
base = Date.now();
} else if (typeof Number(base) == 'number') {
base = Number(base);
} else {
base = parseString(base);
}
if (Number(str)) {
return Number(str) + base;
}
// Lowercase the string
str = str.toLowerCase();
// Turn the base into a date, too
base_date = new Date(base);
// Start at the given base
result = base;
// Do additions by default
add = true;
// Reset the state of the regex
rx_duration.lastIndex = 0;
// Extract all the durations
while (entry = rx_duration.exec(str)) {
if (entry[1]) {
type = entry[1];
result = Number(lastNext(new Date(result), entry[1], entry[2], base_date));
continue;
}
if (!entry[5]) {
name = entry[0];
if (name == '+') {
add = true;
} else {
add = false;
}
if (date_tokens[name]) {
d = new Date(result);
result = Number(date_tokens[name](d, base_date));
} else if (days[name]) {
d = new Date(result);
lastNext(d, 0, name, base_date);
result = Number(d);
}
continue;
}
unit = Collection.Date.getUnitMs(entry[5]);
if (!unit) {
continue;
}
val = parseFloat(entry[4], 10) * unit;
if (add) {
result += val;
} else {
result -= val;
}
}
return result;
});
/**
* Parse durations to milliseconds
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.5.7
* @version 0.5.7
*
* @param {String} str
* @param {String} base
*
* @return {Number}
*/
Blast.defineStatic('Date', 'parseDuration', function parseDuration(str, base) {
return Blast.Bound.Date.parseStringToTime(str, 0);
});
/**
* Return a clone of the current date

@@ -177,3 +387,3 @@ *

* @since 0.1.4
* @version 0.1.4
* @version 0.5.7
*

@@ -191,2 +401,4 @@ * @param {Number} amount

newtime = this.valueOf();
if (typeof amount === 'string') {

@@ -196,4 +408,9 @@ if (typeof unit == 'string') {

} else {
unit = amount;
amount = 1;
if (/\d/.test(amount)) {
this.setTime(newtime + Collection.Date.parseDuration(amount));
return this;
} else {
unit = amount;
amount = 1;
}
}

@@ -205,11 +422,4 @@ } else if (amount == null) {

newtime = this.valueOf();
unit = String(unit).toLowerCase();
unittime = Collection.Date.getUnitMs(unit);
unittime = msUnits[unit];
if (!unittime) {
unittime = msUnits[Bound.String.singularize(unit)] || 0;
}
newtime += unittime * amount;

@@ -227,3 +437,3 @@

* @since 0.1.4
* @version 0.1.4
* @version 0.5.7
*

@@ -245,4 +455,9 @@ * @param {Number} amount

} else {
unit = amount;
amount = 1;
if (/\d/.test(amount)) {
this.setTime(this.valueOf() + Collection.Date.parseDuration(amount) * -1);
return this;
} else {
unit = amount;
amount = 1;
}
}

@@ -255,2 +470,147 @@ }

/**
* Go to a next date
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.5.7
* @version 0.5.7
*
* @param {String} unit year, month, week, day, ...
*
* @return {Date}
*/
Blast.definePrototype('Date', 'next', function next(unit) {
return lastNext(this, +1, unit);
});
/**
* Go to a previous date
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.5.7
* @version 0.5.7
*
* @param {String} unit year, month, week, day, ...
*
* @return {Date}
*/
Blast.definePrototype('Date', 'previous', function previous(unit) {
return lastNext(this, -1, unit);
});
/**
* Perform a last/next unit change
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.5.7
* @version 0.5.7
*
* @param {Date} date
* @param {String} modifier -1 for last, +1 for next
* @param {String} day
* @param {Date} base_date
*
* @return {Date}
*/
function lastNext(date, modifier, unit, base_date) {
var temp;
if (!unit) {
return date;
}
// Lowercase the unit
unit = unit.toLowerCase();
if (typeof modifier == 'string') {
switch (modifier) {
case 'next':
modifier = +1;
break;
case 'last':
case 'previous':
modifier = -1;
break;
case 'this':
modifier = 0;
}
}
// If it's a day of the week, go to that date
if (days[unit] != null) {
return lastNextDay(date, modifier, unit, base_date);
}
if (unit == 'month') {
// Get the current value
temp = date.getMonth();
date.setMonth(date.getMonth() + modifier);
// Catch going earlier than the previous month or over the next
if (temp == date.getMonth() || temp + 1 != date.getMonth()) {
date.setDate(0);
}
} else {
Blast.Bound.Date.add(date, modifier, unit);
}
return date;
}
/**
* Go to a last/next day
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.5.7
* @version 0.5.7
*
* @param {Date} date
* @param {String} modifier -1 for last, +1 for next
* @param {String} day
* @param {Date} base_date
*
* @return {Date}
*/
function lastNextDay(date, modifier, day, base_date) {
var current_iso_day,
current_day,
iso_day,
diff,
nr;
// Get the day count
nr = days[day];
// Get the current day
current_day = date.getDay();
// Calculate the difference
diff = nr - current_day;
if (diff == 0) {
diff = 7 * modifier;
} else if (diff > 0 && modifier < 0) {
diff -= 7;
} else if (diff < 0 && modifier > 0) {
diff += 7;
} else if (diff && modifier == 0) {
// Sunday is a special case
if (current_day == 0) {
diff -= 7;
}
}
diff = date.getDate() + diff;
date.setDate(diff);
return date;
}
/**
* Set the current date to the start of a unit of time

@@ -257,0 +617,0 @@ *

@@ -230,3 +230,3 @@ module.exports = function BlastInitLoader(modifyPrototype) {

'String',
'URL'
'RURL'
];

@@ -1124,2 +1124,4 @@

var proto_keys;
// Make sure the bound collection object exists

@@ -1135,13 +1137,36 @@ if (!Blast.Bound[className]) {

if (StaticClass._blast_class) {
proto_keys = Object.getOwnPropertyNames(StaticClass.prototype);
} else if (StaticClass.prototype) {
proto_keys = Object.keys(StaticClass.prototype);
} else {
return;
}
// Add all the prototype functions (if no static version exists already)
Collection.Object.each(StaticClass.prototype, function eachProperty(PrototypeFunction, functionName) {
Collection.Object.each(proto_keys, function eachProperty(name, index) {
var PrototypeFunction,
descriptor;
descriptor = Object.getOwnPropertyDescriptor(StaticClass.prototype, name);
if (!descriptor || !descriptor.value) {
return;
}
PrototypeFunction = descriptor.value;
if (typeof PrototypeFunction != 'function') {
return;
}
// If there is a static function with the same name,
// it gets precedence!
// @version 0.3.7
if (Object.hasOwnProperty.call(StaticClass, functionName)) {
if (Object.hasOwnProperty.call(StaticClass, name)) {
return;
}
Blast.Bound[className][functionName] = Collection.Function.prototype.unmethodize.call(PrototypeFunction, functionName);
Blast.Bound[className][name] = Collection.Function.prototype.unmethodize.call(PrototypeFunction, name);
});

@@ -1148,0 +1173,0 @@ });

@@ -129,2 +129,32 @@ module.exports = function BlastObject(Blast, Collection, Bound, Obj) {

/**
* Stringify primitives only
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.5.7
* @version 0.5.7
*
* @param {Mixed} arg
*
* @return {String}
*/
Blast.defineStatic('Object', 'stringifyPrimitive', function stringifyPrimitive(arg) {
switch (typeof arg) {
case 'string':
return arg;
case 'number':
if (isFinite(arg)) {
return arg;
}
break;
case 'boolean':
return arg ? 'true' : 'false';
}
return '';
});
/**
* Get the size (length) of any given variable.

@@ -213,9 +243,14 @@ * The return value is always a number, so 0 for invalid arguments

*
* @param {Object} obj The object to flatten
* @param {Object} obj The object to flatten
* @param {String|Array} divider The divider to use (.)
* @param {Number} level
*
* @return {Object}
*/
Blast.defineStatic('Object', 'flatten', function flatten(obj) {
Blast.defineStatic('Object', 'flatten', function flatten(obj, divider, level) {
var result = {},
var divider_start,
divider_end,
new_key,
result = {},
temp,

@@ -225,2 +260,15 @@ key,

if (level == null) {
level = 0;
}
if (!divider) {
divider_start = '.';
} else if (typeof divider == 'string') {
divider_start = divider;
} else if (Array.isArray(divider)) {
divider_start = divider[0];
divider_end = divider[1];
}
for (key in obj) {

@@ -231,4 +279,4 @@

if (Obj.isPlainObject(obj[key])) {
temp = flatten(obj[key]);
if (Obj.isPlainObject(obj[key]) || Array.isArray(obj[key])) {
temp = flatten(obj[key], divider, level + 1);

@@ -241,3 +289,24 @@ // Inject the keys of the sub-object into the result

result[key + '.' + sub] = temp[sub];
if (divider_end) {
new_key = key;
// The root does not have an end divider:
// For example: root[child]
if (level) {
new_key += divider_end;
}
new_key += divider_start + sub;
// If we're back in the root,
// make sure it has an ending divider
if (!level) {
new_key += divider_end;
}
} else {
new_key = key + divider_start + sub;
}
result[new_key] = temp[sub];
}

@@ -452,5 +521,5 @@ } else if (Obj.isPrimitiveObject(obj[key])) {

* @since 0.1.4
* @version 0.5.0
* @version 0.5.7
*/
Blast.defineStatic('Object', 'setPath', function setPath(obj, path, value, skipLastEntry) {
Blast.defineStatic('Object', 'setPath', function setPath(obj, path, value, skipLastEntry, allow_prototype) {

@@ -473,2 +542,6 @@ var argLength = arguments.length,

if (allow_prototype == null) {
allow_prototype = true;
}
// Set out current position

@@ -482,2 +555,6 @@ here = obj;

key = here.length;
if (key == null) {
key = Obj.getNextIndex(here);
}
}

@@ -506,2 +583,6 @@

if (!allow_prototype && !Object.hasOwnProperty.call(here, key)) {
return obj;
}
here = here[key];

@@ -514,30 +595,38 @@ }

/**
* Extract form path info
* Get the next index for this object
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.1.11
* @version 0.1.11
* @since 0.5.7
* @version 0.5.7
*
* @param {String} str
* @param {Object} obj
*
* @return {Number}
*/
function getFormPathArray(str) {
var root_name = /^(.*?)(?:\[|$)/,
prop_name = /(?:\[(.*?)\])/g,
properties = [],
temp;
Blast.defineStatic('Object', 'getNextIndex', function getNextIndex(obj) {
temp = root_name.exec(str);
var keys,
key = obj.length,
max,
nr,
i;
// Look for the root name
if (temp && typeof temp[1] !== 'undefined') {
properties.push(temp[1]);
if (key != null) {
return key;
}
// Look for the sub property names
while (temp = prop_name.exec(str)) {
properties.push(temp[1]);
max = -1;
keys = Obj.values(obj);
for (i = 0; i < keys.length; i++) {
nr = Number(keys[i]);
if (nr > max) {
max = nr;
}
}
return properties;
}
return max + 1;
});

@@ -549,3 +638,3 @@ /**

* @since 0.1.11
* @version 0.1.11
* @version 0.5.7
*

@@ -556,3 +645,3 @@ * @param {Object} obj

Blast.defineStatic('Object', 'formPath', function formPath(obj, form_path) {
var path = getFormPathArray(form_path);
var path = Collection.RURL.parseFormPath(form_path);
return Collection.Object.path(obj, path);

@@ -568,6 +657,6 @@ });

* @since 0.1.11
* @version 0.1.11
* @version 0.5.7
*/
Blast.defineStatic('Object', 'setFormPath', function setFormPath(obj, form_path, value, skipLastEntry) {
var path = getFormPathArray(form_path);
var path = Collection.RURL.parseFormPath(form_path);
return Collection.Object.setPath(obj, path, value, skipLastEntry);

@@ -574,0 +663,0 @@ });

@@ -44,3 +44,3 @@ module.exports = function BlastRequest(Blast, Collection) {

* @since 0.2.0
* @version 0.2.0
* @version 0.5.7
*

@@ -52,3 +52,3 @@ * @param {URL|String} url

if (typeof url == 'string') {
this.url = Blast.Bound.URL.parse(url);
this.url = Blast.Classes.RURL.parse(url);
} else {

@@ -91,3 +91,3 @@ this.url = url;

* @since 0.2.0
* @version 0.4.1
* @version 0.5.7
*

@@ -108,6 +108,6 @@ * @param {Object} options

if (typeof options == 'string') {
url = Blast.Bound.URL.parse(options);
url = Blast.Classes.RURL.parse(options);
options = {};
} else if (typeof options.url == 'string') {
url = Blast.Bound.URL.parse(options.url);
url = Blast.Classes.RURL.parse(options.url);
} else {

@@ -194,3 +194,3 @@ url = options.url;

// Override the URL to request
options.url = URL.parse(res.headers['location'], url.protocol + '//' + config.host);
options.url = Blast.Classes.RURL.parse(res.headers['location'], url.protocol + '//' + config.host);

@@ -202,3 +202,3 @@ if (!options.headers) {

// Set the previous URL as the referrer
options.headers.referrer = '' + url;
options.headers.referrer = url.href;

@@ -205,0 +205,0 @@ return that.http_request(options, callback);

@@ -1374,3 +1374,3 @@ module.exports = function BlastString(Blast, Collection, Bound, Obj) {

* @since 0.1.4
* @version 0.5.0
* @version 0.5.7
*

@@ -1403,2 +1403,13 @@ * @param {Object} values

val = values[match[1]];
if (val && typeof val == 'object') {
// If only the default toString is available,
// get the "first" entry of the object
if (val.toString == Object.prototype.toString) {
val = Obj.first(val);
} else {
val = String(val);
}
}
if (val != null) {

@@ -1405,0 +1416,0 @@ result = result.replace(match[0], val);

{
"name": "protoblast",
"description": "Native object expansion library",
"version": "0.5.6",
"version": "0.5.7",
"author": "Jelle De Loecker <jelle@develry.be>",

@@ -31,2 +31,3 @@ "keywords": [

"istanbul" : "^0.4.5",
"jsuri" : "~1.3.1",
"matcha" : "skerit/matcha",

@@ -33,0 +34,0 @@ "mocha" : "^5.0.1",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc