Fixes to utils
This commit is contained in:
14
.vscode/launch.json
vendored
Normal file
14
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Launch Program",
|
||||
"program": "${workspaceFolder}\\utils.js"
|
||||
}
|
||||
]
|
||||
}
|
||||
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"editor.glyphMargin": false,
|
||||
"editor.folding": false,
|
||||
"scm.diffDecorations": "none"
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
# JavaScript Objects, Prototypes and Classes
|
||||
This is a demo code for the Pluralsight course at https://app.pluralsight.com/courses/javascript-objects-prototypes-and-classes
|
||||
This is the demo code for the Pluralsight course at https://app.pluralsight.com/courses/javascript-objects-prototypes-and-classes
|
||||
|
||||
7
demo.js
7
demo.js
@@ -1,3 +1,6 @@
|
||||
'use strict';
|
||||
'use strict';
|
||||
(function() {
|
||||
|
||||
display('Hello, world!!!');
|
||||
|
||||
|
||||
})();
|
||||
45
utils.js
45
utils.js
@@ -10,6 +10,12 @@ window.display = function () {
|
||||
domActionBuffer.push({ action: 'display', arguments: arguments });
|
||||
};
|
||||
|
||||
window.displayRegexArray = function () {
|
||||
if (!flushTimeout) flushTimeout = setTimeout(flushDomQueue, 100);
|
||||
|
||||
domActionBuffer.push({ action: 'displayRegexArray', arguments: arguments });
|
||||
};
|
||||
|
||||
window.clearDisplay = function () {
|
||||
if (!flushTimeout) flushTimeout = setTimeout(flushDomQueue, 100);
|
||||
|
||||
@@ -25,11 +31,15 @@ function doClearDisplay() {
|
||||
|
||||
function doDisplay() {
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
if (typeof arguments[i] === 'object') displayObject(arguments[i]);
|
||||
if (typeof arguments[i] === 'object') displayObject(arguments[i], false);
|
||||
else displayValue(arguments[i], true);
|
||||
}
|
||||
}
|
||||
|
||||
function doDisplayRegexArray() {
|
||||
displayObject(arguments[0], true)
|
||||
}
|
||||
|
||||
function flushDomQueue() {
|
||||
flushTimeout = null;
|
||||
|
||||
@@ -42,6 +52,9 @@ function flushDomQueue() {
|
||||
case 'display':
|
||||
doDisplay(...domAction.arguments);
|
||||
break;
|
||||
case 'displayRegexArray':
|
||||
doDisplayRegexArray(...domAction.arguments);
|
||||
break;
|
||||
case 'clearDisplay':
|
||||
doClearDisplay();
|
||||
break;
|
||||
@@ -59,12 +72,36 @@ function displayValue(value, addMargin, addPadding) {
|
||||
outputTag.appendChild(div);
|
||||
}
|
||||
|
||||
function displayObject(object) {
|
||||
function displayObject(object, regexArray) {
|
||||
if (object == null) return displayValue('null');
|
||||
if (getTypeName(object) === 'Array' && !regexArray) {
|
||||
let appendedArrayValues = object.reduce((acc, cur) => acc+=cur+',', '')
|
||||
if (appendedArrayValues.length > 0)
|
||||
appendedArrayValues = appendedArrayValues.substr(0, appendedArrayValues.length - 1)
|
||||
displayValue('[' + appendedArrayValues + ']')
|
||||
if (Object.keys(object).length > object.length) {
|
||||
displayValue(' ')
|
||||
displayValue('Additional array properties:')
|
||||
}
|
||||
for (let propertyName in object)
|
||||
{
|
||||
if (!Number.isInteger(+propertyName) && object[propertyName] !== undefined)
|
||||
displayValue(' ' + propertyName + ": " + object[propertyName])
|
||||
else if (typeof object[propertyName] === 'object', false) {
|
||||
return displayObject()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
displayValue(getTypeName(object) + ' {');
|
||||
for (var propertyName in object) {
|
||||
if (propertyName != 'constructor') {
|
||||
displayValue(propertyName + ': ' + object[propertyName], false, true);
|
||||
if (typeof object[propertyName] === 'object', false)
|
||||
displayObject(object[propertyName])
|
||||
else if (propertyName != 'constructor' && (!regexArray || object[propertyName] !== undefined)) {
|
||||
let prefix = Number.isInteger(+propertyName) && regexArray ? '[' : ''
|
||||
let suffix = Number.isInteger(+propertyName) && regexArray ? ']' : ''
|
||||
displayValue(prefix + propertyName + suffix + ': ' + object[propertyName], false, true);
|
||||
}
|
||||
}
|
||||
displayValue('}', true);
|
||||
|
||||
Reference in New Issue
Block a user