From 5b39d4b2da3cda2137db7cc68b595ba27ca2c214 Mon Sep 17 00:00:00 2001 From: Hugh Willson Date: Tue, 25 Jul 2017 21:32:42 -0400 Subject: [PATCH] Remove merged in underscore usage and update to ES5/ES6. --- packages/ejson/ejson.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/ejson/ejson.js b/packages/ejson/ejson.js index b989e3b124..9102083739 100644 --- a/packages/ejson/ejson.js +++ b/packages/ejson/ejson.js @@ -105,22 +105,31 @@ const builtinConverters = [ }, }, { // RegExp - matchJSONValue: function (obj) { - return _.has(obj, '$regexp') && _.has(obj, '$flags') && _.size(obj) === 2; + matchJSONValue(obj) { + return hasOwn(obj, '$regexp') + && hasOwn(obj, '$flags') + && Object.keys(obj).length === 2; }, - matchObject: function (obj) { + matchObject(obj) { return obj instanceof RegExp; }, - toJSONValue: function (regexp) { - return { $regexp: regexp.source, $flags: regexp.flags }; + toJSONValue(regexp) { + return { + $regexp: regexp.source, + $flags: regexp.flags + }; + }, + fromJSONValue(obj) { + // Replaces duplicate / invalid flags. + return new RegExp( + obj.$regexp, + obj.$flags + // Cut off flags at 50 chars to avoid abusing RegExp for DOS. + .slice(0, 50) + .replace(/[^gimuy]/g,'') + .replace(/(.)(?=.*\1)/g, '') + ); }, - fromJSONValue: function (obj) { - // replaces duplicate / invalid flags - // cut of flags to 50 chars to avoid abusing regex for DOS - return new RegExp(obj.$regexp, obj.$flags.substr(0, 50) - .replace(/[^gimuy]/g,'') - .replace(/(.)(?=.*\1)/g, '')); - } }, { // NaN, Inf, -Inf. (These are the only objects with typeof !== 'object' // which we match.)