Skip to content

Commit

Permalink
Fixed _.get()
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwieds committed Apr 17, 2024
1 parent ad56bfb commit ce6063f
Show file tree
Hide file tree
Showing 6 changed files with 1,554 additions and 21 deletions.
6 changes: 4 additions & 2 deletions lib/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Debug(utilObj) {
Debug.promise = function(status, data, options) {
options = options || {};
options.wait = options.wait || {enabled: false};
return new Promise((resolve, reject) => {
return new Promise(function (resolve, reject) {
if (status == 'resolve') {
if (options.wait.enabled) {
Debug.wait(options.wait.msec, options.wait.range)
Expand Down Expand Up @@ -48,7 +48,9 @@ Debug.wait = function(msec, range) {
msec = msec + randomNumPlus - randomNumMinus;
msec = (msec <= 0) ? 50 : msec;
console.log('[DEBUG] waiting...', msec);
return new Promise(resolve => setTimeout(resolve, msec));
return new Promise(function (resolve, reject) {
setTimeout(resolve, msec);
});
}

module.exports = Debug;
2 changes: 1 addition & 1 deletion lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Dom.loadScript = function(options, callback) {

// Set the script attributes
options.attributes
.forEach(attribute => {
.forEach(function (attribute) {
s.setAttribute(attribute.name, attribute.value);
});

Expand Down
53 changes: 39 additions & 14 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ function Utilities(utilObj) {
this.utilities = utilObj;
}

/* https://gist.github.com/jeneg/9767afdcca45601ea44930ea03e0febf */
Utilities.get = function (obj, path, def) {
var fullPath = (path || '')
.replace(/\[/g, '.')
.replace(/]/g, '')
.split('.')
.filter(Boolean);
Utilities.get = function (object, path, defaultValue) {
// If the path is not defined or it has false value
if (!path) {
return defaultValue;
}

return fullPath.every(everyFunc) ? obj : def;
// Check if the path is a string or array. If it is a string, convert it to array
const pathArray = Array.isArray(path) ? path : path.split('.');

function everyFunc(step) {
// return !(step && (obj = obj[step]) === undefined);
// console.log(' CHECK > ', !(step && (obj = obj[step]) === undefined));
// console.log('step', step, 'obj', obj, 'objstep', obj[step]);
// return !(step && (obj = obj[step]) === undefined);
return !(step && (obj = obj[step]) === undefined);
// For each item in the path, dig into the object
let currentObject = object;
for (const key of pathArray) {
if (!currentObject || !currentObject.hasOwnProperty(key)) {
return defaultValue;
}
currentObject = currentObject[key];
}

// Return the value
return currentObject === undefined ? defaultValue : currentObject;
}

/* https://stackoverflow.com/questions/54733539/javascript-implementation-of-lodash-set-method */
Expand All @@ -48,6 +51,28 @@ Utilities.set = function (obj, path, value) {
return obj; // Return the top-level object to allow chaining
}

/* https://gist.github.com/jeneg/9767afdcca45601ea44930ea03e0febf */
Utilities.getLegacy = function (obj, path, def) {
if (!path) {
return def;
}
var fullPath = (path || '')
.replace(/\[/g, '.')
.replace(/]/g, '')
.split('.')
.filter(Boolean);

return fullPath.every(everyFunc) ? obj : def;

function everyFunc(step) {
// return !(step && (obj = obj[step]) === undefined);
// console.log(' CHECK > ', !(step && (obj = obj[step]) === undefined));
// console.log('step', step, 'obj', obj, 'objstep', obj[step]);
// return !(step && (obj = obj[step]) === undefined);
return !(step && (obj = obj[step]) === undefined);
}
}

// https://dzone.com/articles/cross-browser-javascript-copy-and-paste
// https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f
Utilities.clipboardCopy = function (input) {
Expand Down
Loading

0 comments on commit ce6063f

Please sign in to comment.