Skip to content

Commit

Permalink
fix: use var instead of let/const for Node.js 4.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Dec 30, 2020
1 parent e3bdd36 commit f050c3a
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint strict:off */
/* eslint no-var: off */
/* eslint no-redeclare: off */

const stringToParts = require('./stringToParts');
var stringToParts = require('./stringToParts');

// These properties are special and can open client libraries to security
// issues
const ignoreProperties = ['__proto__', 'constructor', 'prototype'];
var ignoreProperties = ['__proto__', 'constructor', 'prototype'];

/**
* Returns the value of object `o` at the given `path`.
Expand Down Expand Up @@ -35,7 +37,7 @@ const ignoreProperties = ['__proto__', 'constructor', 'prototype'];
*/

exports.get = function(path, o, special, map) {
let lookup;
var lookup;

if ('function' == typeof special) {
if (special.length < 2) {
Expand All @@ -49,23 +51,23 @@ exports.get = function(path, o, special, map) {

map || (map = K);

const parts = 'string' == typeof path
var parts = 'string' == typeof path
? stringToParts(path)
: path;

if (!Array.isArray(parts)) {
throw new TypeError('Invalid `path`. Must be either string or array');
}

let obj = o,
var obj = o,
part;

for (let i = 0; i < parts.length; ++i) {
for (var i = 0; i < parts.length; ++i) {
part = parts[i];

if (Array.isArray(obj) && !/^\d+$/.test(part)) {
// reading a property from the array items
const paths = parts.slice(i);
var paths = parts.slice(i);

// Need to `concat()` to avoid `map()` calling a constructor of an array
// subclass
Expand All @@ -79,7 +81,7 @@ exports.get = function(path, o, special, map) {
if (lookup) {
obj = lookup(obj, part);
} else {
const _from = special && obj[special] ? obj[special] : obj;
var _from = special && obj[special] ? obj[special] : obj;
obj = _from instanceof Map ?
_from.get(part) :
_from[part];
Expand All @@ -99,17 +101,17 @@ exports.get = function(path, o, special, map) {
*/

exports.has = function(path, o) {
const parts = typeof path === 'string' ?
var parts = typeof path === 'string' ?
stringToParts(path) :
path;

if (!Array.isArray(parts)) {
throw new TypeError('Invalid `path`. Must be either string or array');
}

const len = parts.length;
let cur = o;
for (let i = 0; i < len; ++i) {
var len = parts.length;
var cur = o;
for (var i = 0; i < len; ++i) {
if (cur == null || typeof cur !== 'object' || !(parts[i] in cur)) {
return false;
}
Expand All @@ -127,17 +129,17 @@ exports.has = function(path, o) {
*/

exports.unset = function(path, o) {
const parts = typeof path === 'string' ?
var parts = typeof path === 'string' ?
stringToParts(path) :
path;

if (!Array.isArray(parts)) {
throw new TypeError('Invalid `path`. Must be either string or array');
}

const len = parts.length;
let cur = o;
for (let i = 0; i < len; ++i) {
var len = parts.length;
var cur = o;
for (var i = 0; i < len; ++i) {
if (cur == null || typeof cur !== 'object' || !(parts[i] in cur)) {
return false;
}
Expand Down Expand Up @@ -166,7 +168,7 @@ exports.unset = function(path, o) {
*/

exports.set = function(path, val, o, special, map, _copying) {
let lookup;
var lookup;

if ('function' == typeof special) {
if (special.length < 2) {
Expand All @@ -180,7 +182,7 @@ exports.set = function(path, val, o, special, map, _copying) {

map || (map = K);

const parts = 'string' == typeof path
var parts = 'string' == typeof path
? stringToParts(path)
: path;

Expand All @@ -190,7 +192,7 @@ exports.set = function(path, val, o, special, map, _copying) {

if (null == o) return;

for (let i = 0; i < parts.length; ++i) {
for (var i = 0; i < parts.length; ++i) {
// Silently ignore any updates to `__proto__`, these are potentially
// dangerous if using mpath with unsanitized data.
if (ignoreProperties.indexOf(parts[i]) !== -1) {
Expand All @@ -203,12 +205,11 @@ exports.set = function(path, val, o, special, map, _copying) {
// the array to the one by one to matching positions of the
// current array. Unless the user explicitly opted out by passing
// false, see Automattic/mongoose#6273
const copy = _copying || (/\$/.test(path) && _copying !== false);
let obj = o;
let part;
const len = parts.length - 1;
var copy = _copying || (/\$/.test(path) && _copying !== false),
obj = o,
part;

for (let i = 0; i < len; ++i) {
for (var i = 0, len = parts.length - 1; i < len; ++i) {
part = parts[i];

if ('$' == part) {
Expand All @@ -220,14 +221,14 @@ exports.set = function(path, val, o, special, map, _copying) {
}

if (Array.isArray(obj) && !/^\d+$/.test(part)) {
const paths = parts.slice(i);
var paths = parts.slice(i);
if (!copy && Array.isArray(val)) {
for (let j = 0; j < obj.length && j < val.length; ++j) {
for (var j = 0; j < obj.length && j < val.length; ++j) {
// assignment of single values of array
exports.set(paths, val[j], obj[j], special || lookup, map, copy);
}
} else {
for (let j = 0; j < obj.length; ++j) {
for (var j = 0; j < obj.length; ++j) {
// assignment of entire value
exports.set(paths, val, obj[j], special || lookup, map, copy);
}
Expand All @@ -238,7 +239,7 @@ exports.set = function(path, val, o, special, map, _copying) {
if (lookup) {
obj = lookup(obj, part);
} else {
const _to = special && obj[special] ? obj[special] : obj;
var _to = special && obj[special] ? obj[special] : obj;
obj = _to instanceof Map ?
_to.get(part) :
_to[part];
Expand All @@ -261,8 +262,8 @@ exports.set = function(path, val, o, special, map, _copying) {
if (!copy && Array.isArray(val)) {
_setArray(obj, val, part, lookup, special, map);
} else {
for (let j = 0; j < obj.length; ++j) {
let item = obj[j];
for (var j = 0; j < obj.length; ++j) {
var item = obj[j];
if (item) {
if (lookup) {
lookup(item, part, map(val));
Expand All @@ -289,8 +290,8 @@ exports.set = function(path, val, o, special, map, _copying) {
*/

function _setArray(obj, val, part, lookup, special, map) {
for (let j = 0; j < obj.length && j < val.length; ++j) {
let item = obj[j];
for (var item, j = 0; j < obj.length && j < val.length; ++j) {
item = obj[j];
if (Array.isArray(item) && Array.isArray(val[j])) {
_setArray(item, val[j], part, lookup, special, map);
} else if (item) {
Expand All @@ -310,4 +311,4 @@ function _setArray(obj, val, part, lookup, special, map) {

function K(v) {
return v;
}
}

0 comments on commit f050c3a

Please sign in to comment.