Skip to content

Commit

Permalink
jsonParser objects
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Jan 8, 2025
1 parent 3fc02f6 commit 0cf8092
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 90 deletions.
8 changes: 4 additions & 4 deletions lib/layer/format/maplibre.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default async layer => {

layer.mapview.Map.getTargetElement().prepend(layer.container)

layer.Map = await MaplibreGL({
const Map = await MaplibreGL({
container: layer.container,
pixelRatio: 1,
style: layer.style.URL,
Expand All @@ -80,10 +80,10 @@ export default async layer => {
}
});

if (!layer.Map) return;
if (!Map) return;

// The Maplibre Map control must resize with mapview Map targetElement.
layer.mapview.Map.getTargetElement().addEventListener('resize', () => layer.Map.resize())
layer.mapview.Map.getTargetElement().addEventListener('resize', () => Map.resize())

// Handle layer.style.zIndex deprecation.
if (layer.style.zIndex) {
Expand All @@ -100,7 +100,7 @@ export default async layer => {
layer.container.style.visibility = 'visible';

// adjust view parameters in mapbox
layer.Map.jumpTo({
Map.jumpTo({
center: ol.proj.toLonLat(frameState.viewState.center),
zoom: frameState.viewState.zoom - 1,
bearing: (-frameState.viewState.rotation * 180) / Math.PI,
Expand Down
154 changes: 68 additions & 86 deletions lib/utils/jsonParser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The jsonParser utility is used to parse a locale to be stored as a userLocale.
@module /utils/jsonParser
*/

export default jsonParser
export default jsonParser;

let sourceObjArray, targetObjArray;

Expand All @@ -22,21 +22,20 @@ The jsonParser method resets the arrays to hold parsed objects and removes the p
@returns {Object} Returns a JSON object which can be stored in an object store.
*/
function jsonParser(obj) {

if (typeof obj !== 'object') return;

const jsonObject = {}
const jsonObject = {};

sourceObjArray = []
targetObjArray = []
sourceObjArray = [];
targetObjArray = [];

propertyParser(jsonObject, obj)
propertyParser(jsonObject, obj);

// Remove parser flags.
sourceObjArray.forEach(obj => delete obj.__parsed)
targetObjArray.forEach(obj => delete obj.__parsed)
sourceObjArray.forEach((obj) => delete obj.__parsed);
targetObjArray.forEach((obj) => delete obj.__parsed);

return jsonObject
return jsonObject;
}

/**
Expand All @@ -49,150 +48,134 @@ The propertyParser parses a source object and assigns properties which can be st
@param {Object} source
*/
function propertyParser(target, source) {

if (!isObject(source)) return source;

// The source object must only be parsed once.
source.__parsed = true;

sourceObjArray.push(source)
sourceObjArray.push(source);

targetObjArray.push(target)
targetObjArray.push(target);

// Iterate through the source own enumerable string-keyed property key-value pairs.
for (const [key, value] of Object.entries(source)) {

// The ignoreKeys contain checks against prototype pollution.
if (new Set(['__proto__', 'constructor', 'mapview']).has(key)) {

continue;
}

if (assignValue(target, key, value)) continue;

if (excludeProperty(value)) continue;

if (Array.isArray(value)) {
if (assignValue(target, key, value)) continue;

target[key] = value.map(entry => isObject(entry)
? propertyParser({}, entry)
: entry)
if (Array.isArray(value)) {
target[key] = value.map((entry) =>
isObject(entry) ? propertyParser({}, entry) : entry
);
continue;
}

if (value.hasOwnProperty('__parsed')) {

if (value.key) {
target[key] = value.key
target[key] = value.key;
}
continue;
}

// Value must be an object at this point.
target[key] ??= {}
target[key] ??= {};

// Call recursive merge for target key object.
propertyParser(target[key], value);
}

return target
return target;
}

/**
@function assignValue
@function excludeProperty
@description
Returns true if a true, false, null, string, or num value can be assigned to the target object.
Returns true if undefined value is skipped.
The excludeProperty method returns true if a property should be exluded from the target in the propertyParser iteration.
@param {Object} target
@param {string} key
@param {*} value
@returns {Boolean} True if the value has been assigned.
@returns {Boolean} True if the property should be excluded in regards to the property value.
*/
function assignValue(target, key, value) {

// Prevent prototype polluting assignment.
if (key === '__proto__' || key === 'constructor') return true;

if (value === undefined) {

return true
}

if (value === true) {

target[key] = true
return true
function excludeProperty(value) {
if (value instanceof Function) {
return true;
}

if (value === false) {

target[key] = false
return true
if (value instanceof HTMLElement) {
return true;
}

if (value === null) {
if (value instanceof Object) {
if (
value.type === 'html' &&
Array.isArray(value.template) &&
Array.isArray(value.values)
) {
return true;
}

target[key] = null
return true
// Openlayer objects will have an unique id.
if (Object.hasOwn(value, 'ol_uid')) {
return true;
}
}

if (typeof value === 'string') {

target[key] = value
return true
}
if (Array.isArray(value)) {
// Array contains reference objects which have already been parssed.
if (typeof value[0] === 'object' && value[0].__parsed) {
return true;
}

if (!isNaN(value)) {
// Ignore callback arrays.
if (value[0] instanceof Function) return true;

target[key] = value
return true
// Ignore html template arrays.
if (value[0] instanceof HTMLElement) return true;
}
}

/**
@function excludeProperty
@function assignValue
@description
The excludeProperty method returns true if a property should be exluded from the target in the propertyParser iteration.
Returns true if a true, false, null, string, or num value can be assigned to the target object.
Returns true if undefined value is skipped.
@param {Object} target
@param {string} key
@param {*} value
@returns {Boolean} True if the property should be excluded in regards to the property value.
@returns {Boolean} True if the value has been assigned.
*/
function excludeProperty(value) {
function assignValue(target, key, value) {
// Prevent prototype polluting assignment.
if (key === '__proto__' || key === 'constructor') return true;

// Openlayer objects will have an unique id.
if (Object.hasOwn(value, 'ol_uid')) {
if (value === undefined) {
return true;
}

if (value.type === 'html'
&& Array.isArray(value.template)
&& Array.isArray(value.values)) {

if (value === true) {
target[key] = true;
return true;
}

if (value instanceof HTMLElement) {
if (value === false) {
target[key] = false;
return true;
}

if (value instanceof Function) {
if (value === null) {
target[key] = null;
return true;
}

if (Array.isArray(value)) {

// Array contains reference objects which have already been parssed.
if (typeof value[0] === 'object' && value[0].__parsed) {
return true
}

// Ignore html template arrays.
if (value[0] instanceof HTMLElement) return true;

// Ignore callback arrays.
if (value[0] instanceof Function) return true;
if (typeof value !== 'object') {
target[key] = value;
return true;
}
}

Expand All @@ -204,7 +187,6 @@ Checks whether the item argument is an object but not true, false, null, or an a
@returns {Boolean} True if the item is an object but not true, false, null, or an array.
*/
function isObject(item) {

if (item === true) return false;

if (item === false) return false;
Expand All @@ -214,4 +196,4 @@ function isObject(item) {
if (Array.isArray(item)) return false;

if (typeof item === 'object') return true;
}
}

0 comments on commit 0cf8092

Please sign in to comment.