-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from AlchemyCMS/remove-commonjs
Remove CommonJS package
- Loading branch information
Showing
7 changed files
with
103 additions
and
224 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,132 @@ | ||
'use strict'; | ||
import structuredClone from '@ungap/structured-clone'; | ||
|
||
var structuredClone = require('@ungap/structured-clone'); | ||
|
||
function deserialize(originalResponse) { | ||
function deserialize(originalResponse, options = {}) { | ||
const response = structuredClone(originalResponse); | ||
if (!options) { | ||
options = {}; | ||
} | ||
|
||
const included = response.included || []; | ||
|
||
if (Array.isArray(response.data)) { | ||
return response.data.map(data => { | ||
return parseJsonApiSimpleResourceData(data, included, false); | ||
}); | ||
return response.data.map((data) => { | ||
return parseJsonApiSimpleResourceData(data, included, false) | ||
}) | ||
} else { | ||
return parseJsonApiSimpleResourceData(response.data, included, false); | ||
return parseJsonApiSimpleResourceData( | ||
response.data, | ||
included, | ||
false) | ||
} | ||
} | ||
|
||
function parseJsonApiSimpleResourceData(data, included, useCache, options) { | ||
if (!included.cached) { | ||
included.cached = {}; | ||
} | ||
|
||
if (!(data.type in included.cached)) { | ||
included.cached[data.type] = {}; | ||
} | ||
|
||
if (useCache && data.id in included.cached[data.type]) { | ||
return included.cached[data.type][data.id]; | ||
return included.cached[data.type][data.id] | ||
} | ||
|
||
const attributes = data.attributes || {}; | ||
|
||
const resource = attributes; | ||
resource.id = data.id; | ||
|
||
included.cached[data.type][data.id] = resource; | ||
|
||
if (data.relationships) { | ||
for (const relationName of Object.keys(data.relationships)) { | ||
const relationRef = data.relationships[relationName]; | ||
|
||
if (Array.isArray(relationRef.data)) { | ||
const items = []; | ||
relationRef.data.forEach(relationData => { | ||
const item = findJsonApiIncluded(included, relationData.type, relationData.id); | ||
|
||
relationRef.data.forEach((relationData) => { | ||
const item = findJsonApiIncluded( | ||
included, | ||
relationData.type, | ||
relationData.id); | ||
|
||
items.push(item); | ||
}); | ||
|
||
resource[relationName] = items; | ||
} else if (relationRef && relationRef.data) { | ||
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id); | ||
resource[relationName] = findJsonApiIncluded( | ||
included, | ||
relationRef.data.type, | ||
relationRef.data.id); | ||
} else { | ||
resource[relationName] = null; | ||
} | ||
} | ||
} | ||
return resource; | ||
|
||
return resource | ||
} | ||
|
||
function findJsonApiIncluded(included, type, id, options) { | ||
let found = null; | ||
included.forEach(item => { | ||
|
||
included.forEach((item) => { | ||
if (item.type === type && item.id === id) { | ||
found = parseJsonApiSimpleResourceData(item, included, true); | ||
} | ||
}); | ||
|
||
if (!found) { | ||
found = { | ||
id | ||
}; | ||
found = { id }; | ||
} | ||
return found; | ||
|
||
return found | ||
} | ||
|
||
// Recursively filters all deprecated elements and essences from collection | ||
function filterDeprecatedElements(elements) { | ||
const els = []; | ||
elements.forEach(element => { | ||
|
||
elements.forEach((element) => { | ||
if (element.nested_elements?.length > 0) { | ||
element.nested_elements = filterDeprecatedElements(element.nested_elements); | ||
element.nested_elements = filterDeprecatedElements( | ||
element.nested_elements | ||
); | ||
} | ||
if (element.nestedElements?.length > 0) { | ||
element.nestedElements = filterDeprecatedElements(element.nestedElements); | ||
} | ||
if (element.essences?.length > 0) { | ||
element.essences = element.essences.filter(essence => { | ||
return !essence.deprecated; | ||
element.essences = element.essences.filter((essence) => { | ||
return !essence.deprecated | ||
}); | ||
} | ||
if (!element.deprecated) { | ||
els.push(element); | ||
} | ||
}); | ||
return els; | ||
|
||
return els | ||
} | ||
|
||
// Returns deserialized page without deprecated content | ||
function deserializePage(pageData) { | ||
const page = deserialize(pageData); | ||
page.elements = filterDeprecatedElements(page.elements); | ||
return page; | ||
return page | ||
} | ||
|
||
// Returns deserialized pages without deprecated content | ||
function deserializePages(pagesData) { | ||
const pages = deserialize(pagesData); | ||
pages.forEach(page => { | ||
pages.forEach((page) => { | ||
page.elements = filterDeprecatedElements(page.elements); | ||
}); | ||
return pages; | ||
return pages | ||
} | ||
|
||
exports.deserialize = deserialize; | ||
exports.deserializePage = deserializePage; | ||
exports.deserializePages = deserializePages; | ||
export { deserialize, deserializePage, deserializePages }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,90 @@ | ||
'use strict'; | ||
import structuredClone from '@ungap/structured-clone'; | ||
|
||
var structuredClone = require('@ungap/structured-clone'); | ||
|
||
function deserialize(originalResponse) { | ||
function deserialize(originalResponse, options = {}) { | ||
const response = structuredClone(originalResponse); | ||
if (!options) { | ||
options = {}; | ||
} | ||
|
||
const included = response.included || []; | ||
|
||
if (Array.isArray(response.data)) { | ||
return response.data.map(data => { | ||
return parseJsonApiSimpleResourceData(data, included, false); | ||
}); | ||
return response.data.map((data) => { | ||
return parseJsonApiSimpleResourceData(data, included, false) | ||
}) | ||
} else { | ||
return parseJsonApiSimpleResourceData(response.data, included, false); | ||
return parseJsonApiSimpleResourceData( | ||
response.data, | ||
included, | ||
false) | ||
} | ||
} | ||
|
||
function parseJsonApiSimpleResourceData(data, included, useCache, options) { | ||
if (!included.cached) { | ||
included.cached = {}; | ||
} | ||
|
||
if (!(data.type in included.cached)) { | ||
included.cached[data.type] = {}; | ||
} | ||
|
||
if (useCache && data.id in included.cached[data.type]) { | ||
return included.cached[data.type][data.id]; | ||
return included.cached[data.type][data.id] | ||
} | ||
|
||
const attributes = data.attributes || {}; | ||
|
||
const resource = attributes; | ||
resource.id = data.id; | ||
|
||
included.cached[data.type][data.id] = resource; | ||
|
||
if (data.relationships) { | ||
for (const relationName of Object.keys(data.relationships)) { | ||
const relationRef = data.relationships[relationName]; | ||
|
||
if (Array.isArray(relationRef.data)) { | ||
const items = []; | ||
relationRef.data.forEach(relationData => { | ||
const item = findJsonApiIncluded(included, relationData.type, relationData.id); | ||
|
||
relationRef.data.forEach((relationData) => { | ||
const item = findJsonApiIncluded( | ||
included, | ||
relationData.type, | ||
relationData.id); | ||
|
||
items.push(item); | ||
}); | ||
|
||
resource[relationName] = items; | ||
} else if (relationRef && relationRef.data) { | ||
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id); | ||
resource[relationName] = findJsonApiIncluded( | ||
included, | ||
relationRef.data.type, | ||
relationRef.data.id); | ||
} else { | ||
resource[relationName] = null; | ||
} | ||
} | ||
} | ||
return resource; | ||
|
||
return resource | ||
} | ||
|
||
function findJsonApiIncluded(included, type, id, options) { | ||
let found = null; | ||
included.forEach(item => { | ||
|
||
included.forEach((item) => { | ||
if (item.type === type && item.id === id) { | ||
found = parseJsonApiSimpleResourceData(item, included, true); | ||
} | ||
}); | ||
|
||
if (!found) { | ||
found = { | ||
id | ||
}; | ||
found = { id }; | ||
} | ||
return found; | ||
|
||
return found | ||
} | ||
|
||
exports.deserialize = deserialize; | ||
export { deserialize }; |
Oops, something went wrong.