Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

amf-json-ld-lib

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

amf-json-ld-lib - npm Package Compare versions

Comparing version 0.0.7 to 0.0.8

2

dist/main.js

@@ -1,2 +0,2 @@

var t=function(){};t.prototype.validate=function(t){if(void 0===t)throw new Error("Node must not be undefined");if(Array.isArray(t))throw new Error("Node must not be an array");if(void 0===t["@id"])throw new Error("Node must have an @id")},t.empty=function(){return new t},t.prototype.withNode=function(t){this.validate(t);var r=t["@id"];if(void 0!==this[r])throw new Error("Cannot add node with duplicate id "+t["@id"]+" to dictionary");return this[r]=t,this},t.prototype.withNodes=function(t){var r=this;return t.forEach(function(t){return r.withNode(t)}),this},t.prototype.update=function(t){this.validate(t);var r=t["@id"];if(void 0===this[r])throw new Error("No node to update with id "+t["@id"]+" in dictionary");return this[r]=t,this};var r=function(){};r.targetOrLink=function(t,r){var e=r[t["@id"]];return void 0!==e?e:t},r.isLinkObject=function(t){var r="object"==typeof t,e=!Array.isArray(t),n=void 0!==t["@id"],i=1===Object.keys(t).length;return r&&e&&n&&i},r.isArrayOfLinks=function(t){var r=this;return t.every(function(t){return r.isLinkObject(t)})};var e=function(){};e.embedNodeLinks=function(t,e){return Object.entries(t).forEach(function(n){var i=n[0],o=n[1];"object"==typeof o&&(r.isLinkObject(o)?t[i]=r.targetOrLink(o,e):Array.isArray(o)&&r.isArrayOfLinks(o)&&(t[i]=o.map(function(t){return r.targetOrLink(t,e)})))}),t};var n=function(){};n.expandIris=function(t,r){Object.entries(t).forEach(function(e){var i=e[0],o=e[1];delete t[i];var a=n.expandIri(i,r);t[a]=o})},n.compactIris=function(t,r){Object.entries(t).forEach(function(e){var i=e[0],o=e[1];delete t[i];var a=n.compactIri(i,r);t[a]=o})},n.compactIri=function(t,r){switch(typeof r){case"string":return t.replace(r,"");case"object":if(Array.isArray(r))throw new Error("Context must be either an object or a string");if(this.isCompacted(t,r))return t;try{var e=Object.entries(r).find(function(r){return t.startsWith(r[1])});return t.replace(e[1],e[0]+":")}catch(r){return t}default:throw new Error("Context must be either an object or a string")}},n.expandIri=function(t,r){switch(typeof r){case"string":return""+r+t;case"object":if(Array.isArray(r))throw new Error("Context must be either an object or a string");if(!this.isCompacted(t,r))return t;try{var e=Object.entries(r).find(function(r){return t.startsWith(r[0])});return t.replace(e[0]+":",e[1])}catch(r){return t}default:throw new Error("Context must be either an object or a string")}},n.isCompacted=function(t,r){var e=!0;return Object.values(r).forEach(function(r){t.startsWith(r)&&(e=!1)}),e},n.compareIris=function(t,r,e){return this.expandIri(t,e)===this.expandIri(r,e)};var i=function(){};i.expandStructure=function(t){var r=this;return Object.entries(t).forEach(function(e){var n=e[0],i=e[1];switch(n){case"@id":break;case"@type":r.expandType(t,i);break;default:r.expandValue(t,n,i)}}),t},i.expandType=function(t,r){t["@type"]="string"==typeof r?[r]:r},i.expandValue=function(t,r,e){switch(typeof e){case"string":case"number":case"boolean":t[r]=[{"@value":e}];break;case"object":Array.isArray(e)?e.map(function(t){switch(typeof t){case"string":case"number":case"boolean":return[{"@value":e}];default:return t}}):t[r]=[e]}},i.compactStructure=function(t){var r=this;return Array.isArray(t)?this.compactStructure(t[0]):(Object.entries(t).forEach(function(e){var n=e[0],i=e[1];switch(n){case"@id":break;case"@type":r.compactType(t,i);break;default:r.compactValue(t,n,i)}}),t)},i.compactType=function(t,r){Array.isArray(r)&&1===r.length&&(t["@type"]=r[0])},i.compactValue=function(t,r,e){var n=this;if(1===e.length){var i=e[0];t[r]=this.isWrappedScalar(i)?i["@value"]:i}else e.map(function(t){return n.isWrappedScalar(t)?t["@value"]:t})},i.isWrappedScalar=function(t){return void 0!==t["@value"]};var o=function(t){this.queue=t};o.empty=function(){return new o([])},o.prototype.enqueue=function(t){return new o(this.queue.concat([t]))},o.prototype.dequeue=function(){return new o(this.queue.slice(0,this.queue.length-2))},o.prototype.contains=function(t){return null!=this.queue.find(function(r){return r===t})};var a=function(t,r){void 0===r&&(r=function(){return!0}),this.replacer=t,this.shouldTraverse=r};a.prototype.trim=function(t){this.trim_(t,o.empty())},a.prototype.trim_=function(t,r){var e=this;Object.entries(t).filter(function(t){var r=t[1];return"object"==typeof r&&e.shouldTraverse(t[0],r)}).forEach(function(n){var i=n[0],o=n[1];Array.isArray(o)?t[i]=o.map(function(n){return r.contains(n)||t===n?e.replacer(n):(e.trim_(n,r.enqueue(t)),n)}):r.contains(o)||t===o?t[i]=e.replacer(o):e.trim_(o,r.enqueue(t))})};var c=function(){};c.process=function(r,o){var a=r["@graph"],c=r["@context"],u=t.empty().withNodes(a);a.forEach(function(t){o.embeddedLinks&&e.embedNodeLinks(t,u),o.expandedIris?n.expandIris(t,c):o.compactedIris&&n.compactIris(t,c),o.expandedStructure?i.expandStructure(t):o.compactedStructure&&i.compactStructure(t)});var s=void 0===o.rootNodeId?{"@graph":a}:a.find(function(t){return t["@id"]===o.rootNodeId});return void 0!==c&&(s["@context"]=c),o.expandedStructure?[s]:s},c.stringify=function(t,r,e){return void 0===r&&(r=!1),void 0===e&&(e=!0),e&&new a(function(t){return t["@id"]}).trim(t),JSON.stringify(t,null,r?2:0)};var u=function(){};u.apply=function(){return new u},u.prototype.withCompactedStructure=function(){return this.expandedStructure=!1,this.compactedStructure=!0,this},u.prototype.withRootNode=function(t){return this.rootNodeId=t,this},u.prototype.withExpandedStructure=function(){return this.expandedStructure=!0,this.compactedStructure=!1,this},u.prototype.withExpandedIris=function(){return this.expandedIris=!0,this.compactedIris=!1,this},u.prototype.withCompactedIris=function(){return this.expandedIris=!1,this.compactedStructure=!0,this},u.prototype.withEmbeddedLinks=function(){return this.embeddedLinks=!0,this};var s=function(){};s.expand=function(t){if(this.isInExpandedForm(t))return t;var r=u.apply().withEmbeddedLinks().withCompactedIris().withExpandedStructure().withRootNode("./"),e=c.process(t,r);return s.trimCycles(e),e},s.trimCycles=function(t){new a(function(t){return t["@id"]},function(r,e){var i=null!=e["@type"],o=e["@type"].map(function(r){return n.expandIri(r,t[0]["@context"])}).includes("http://www.w3.org/ns/shacl#Shape");return i&&o}).trim(t)},s.isInExpandedForm=function(t){return void 0===t["@graph"]},exports.JsonLd=c,exports.JsonLdOptions=u,exports.AmfModelExpander=s;
var t=function(){};t.prototype.validate=function(t){if(void 0===t)throw new Error("Node must not be undefined");if(Array.isArray(t))throw new Error("Node must not be an array");if(void 0===t["@id"])throw new Error("Node must have an @id")},t.empty=function(){return new t},t.prototype.withNode=function(t){this.validate(t);var r=t["@id"];if(void 0!==this[r])throw new Error("Cannot add node with duplicate id "+t["@id"]+" to dictionary");return this[r]=t,this},t.prototype.withNodes=function(t){var r=this;return t.forEach(function(t){return r.withNode(t)}),this},t.prototype.update=function(t){this.validate(t);var r=t["@id"];if(void 0===this[r])throw new Error("No node to update with id "+t["@id"]+" in dictionary");return this[r]=t,this};var r=function(){};r.targetOrLink=function(t,r){var e=r[t["@id"]];return void 0!==e?e:t},r.isLinkObject=function(t){var r="object"==typeof t,e=!Array.isArray(t),n=void 0!==t["@id"],i=1===Object.keys(t).length;return r&&e&&n&&i},r.isArrayOfLinks=function(t){var r=this;return t.every(function(t){return r.isLinkObject(t)})};var e=function(){};e.embedNodeLinks=function(t,e,n){var i=n||function(){return!0};return Object.entries(t).filter(function(t){return i(t[0],t[1])}).forEach(function(n){var i=n[0],o=n[1];"object"==typeof o&&(r.isLinkObject(o)?t[i]=r.targetOrLink(o,e):Array.isArray(o)&&r.isArrayOfLinks(o)&&(t[i]=o.map(function(t){return r.targetOrLink(t,e)})))}),t};var n=function(){};n.expandIris=function(t,r){Object.entries(t).forEach(function(e){var i=e[0],o=e[1];delete t[i];var a=n.expandIri(i,r);t[a]=o})},n.compactIris=function(t,r){Object.entries(t).forEach(function(e){var i=e[0],o=e[1];delete t[i];var a=n.compactIri(i,r);t[a]=o})},n.compactIri=function(t,r){switch(typeof r){case"string":return t.replace(r,"");case"object":if(Array.isArray(r))throw new Error("Context must be either an object or a string");if(this.isCompacted(t,r))return t;try{var e=Object.entries(r).find(function(r){return t.startsWith(r[1])});return t.replace(e[1],e[0]+":")}catch(r){return t}default:throw new Error("Context must be either an object or a string")}},n.expandIri=function(t,r){switch(typeof r){case"string":return""+r+t;case"object":if(Array.isArray(r))throw new Error("Context must be either an object or a string");if(!this.isCompacted(t,r))return t;try{var e=Object.entries(r).find(function(r){return t.startsWith(r[0])});return t.replace(e[0]+":",e[1])}catch(r){return t}default:throw new Error("Context must be either an object or a string")}},n.isCompacted=function(t,r){var e=!0;return Object.values(r).forEach(function(r){t.startsWith(r)&&(e=!1)}),e},n.compareIris=function(t,r,e){return this.expandIri(t,e)===this.expandIri(r,e)};var i=function(){};i.expandStructure=function(t){var r=this;return Object.entries(t).forEach(function(e){var n=e[0],i=e[1];switch(n){case"@id":break;case"@type":r.expandType(t,i);break;default:r.expandValue(t,n,i)}}),t},i.expandType=function(t,r){t["@type"]="string"==typeof r?[r]:r},i.expandValue=function(t,r,e){switch(typeof e){case"string":case"number":case"boolean":t[r]=[{"@value":e}];break;case"object":Array.isArray(e)?e.map(function(t){switch(typeof t){case"string":case"number":case"boolean":return[{"@value":e}];default:return t}}):t[r]=[e]}},i.compactStructure=function(t){var r=this;return Array.isArray(t)?this.compactStructure(t[0]):(Object.entries(t).forEach(function(e){var n=e[0],i=e[1];switch(n){case"@id":break;case"@type":r.compactType(t,i);break;default:r.compactValue(t,n,i)}}),t)},i.compactType=function(t,r){Array.isArray(r)&&1===r.length&&(t["@type"]=r[0])},i.compactValue=function(t,r,e){var n=this;if(1===e.length){var i=e[0];t[r]=this.isWrappedScalar(i)?i["@value"]:i}else e.map(function(t){return n.isWrappedScalar(t)?t["@value"]:t})},i.isWrappedScalar=function(t){return void 0!==t["@value"]};var o=function(){};o.process=function(r,o){var a=r["@graph"],c=r["@context"],u=t.empty().withNodes(a);a.forEach(function(t){o.embeddedLinks&&e.embedNodeLinks(t,u,o.linkEmbeddingFilter),o.expandedIris?n.expandIris(t,c):o.compactedIris&&n.compactIris(t,c),o.expandedStructure?i.expandStructure(t):o.compactedStructure&&i.compactStructure(t)});var s=void 0===o.rootNodeId?{"@graph":a}:a.find(function(t){return t["@id"]===o.rootNodeId});return void 0!==c&&(s["@context"]=c),o.expandedStructure?[s]:s},o.stringify=function(t,r){return void 0===r&&(r=!1),JSON.stringify(t,null,r?2:0)};var a=function(){};a.apply=function(){return new a},a.prototype.withCompactedStructure=function(){return this.expandedStructure=!1,this.compactedStructure=!0,this},a.prototype.withRootNode=function(t){return this.rootNodeId=t,this},a.prototype.withExpandedStructure=function(){return this.expandedStructure=!0,this.compactedStructure=!1,this},a.prototype.withExpandedIris=function(){return this.expandedIris=!0,this.compactedIris=!1,this},a.prototype.withCompactedIris=function(){return this.expandedIris=!1,this.compactedStructure=!0,this},a.prototype.withEmbeddedLinks=function(t){return this.linkEmbeddingFilter=t,this.embeddedLinks=!0,this};var c=function(){};c.expand=function(t){if(this.isInExpandedForm(t))return t;var r=a.apply().withEmbeddedLinks(function(t,r){return!t.endsWith("fixPoint")}).withCompactedIris().withExpandedStructure().withRootNode("./");return o.process(t,r)},c.isInExpandedForm=function(t){return void 0===t["@graph"]},exports.JsonLd=o,exports.JsonLdOptions=a,exports.AmfModelExpander=c;
//# sourceMappingURL=main.js.map
{
"name": "amf-json-ld-lib",
"version": "0.0.7",
"version": "0.0.8",
"source": "src/main.ts",

@@ -5,0 +5,0 @@ "main": "dist/main.js",

import JsonLdOptions from "./JsonLdOptions"
import JsonLd from "./JsonLd"
import CycleTrimmer from "./utils/CycleTrimmer"
import IriOperations from "./operations/IriOperations"

@@ -9,11 +7,10 @@ export default class AmfModelExpander {

if (!this.isInExpandedForm(jsonld)) {
const linkEmbeddingFilter = (key: string, value: any) => !key.endsWith("fixPoint")
const options = JsonLdOptions
.apply()
.withEmbeddedLinks()
.withEmbeddedLinks(linkEmbeddingFilter)
.withCompactedIris()
.withExpandedStructure()
.withRootNode("./")
const processedJsonLd = JsonLd.process(jsonld, options)
AmfModelExpander.trimCycles(processedJsonLd)
return processedJsonLd
return JsonLd.process(jsonld, options)
} else {

@@ -24,16 +21,2 @@ return jsonld

private static trimCycles(jsonLd: object) {
const replacer = (node: object) => { return node['@id'] }
// We only traverse shapes
const shouldTraverse = (key: string, value: object) => {
const hasType = value["@type"] != undefined
const isShape = value["@type"]
.map(typeIri => IriOperations.expandIri(typeIri, jsonLd[0]["@context"]))
.includes("http://www.w3.org/ns/shacl#Shape")
return hasType && isShape
}
const trimmer = new CycleTrimmer(replacer, shouldTraverse)
trimmer.trim(jsonLd)
}
private static isInExpandedForm(jsonld: object) {

@@ -40,0 +23,0 @@ return jsonld['@graph'] === undefined

@@ -6,3 +6,2 @@ import LinkDictionary from "./model/LinkDictionary";

import NodeOperations from "./operations/NodeOperations";
import CycleTrimmer from "./utils/CycleTrimmer"

@@ -18,3 +17,3 @@ export default class JsonLd {

if (options.embeddedLinks) {
LinkOperations.embedNodeLinks(node, dictionary)
LinkOperations.embedNodeLinks(node, dictionary, options.linkEmbeddingFilter)
}

@@ -53,10 +52,3 @@

static stringify(jsonld: object, prettyPrint: boolean = false, replaceCyles: boolean = true) {
if (replaceCyles) {
const replacer = (node: object) => {
return node['@id']
}
const trimmer = new CycleTrimmer(replacer)
trimmer.trim(jsonld)
}
static stringify(jsonld: object, prettyPrint: boolean = false) {
return JSON.stringify(jsonld, null, prettyPrint ? 2 : 0);

@@ -63,0 +55,0 @@ }

@@ -8,2 +8,3 @@ export default class JsonLdOptions {

public rootNodeId?: string
public linkEmbeddingFilter?: (key: string, value: any) => boolean

@@ -43,3 +44,4 @@ static apply() {

withEmbeddedLinks() {
withEmbeddedLinks(entryFilter?: (key: string, value: any) => boolean) {
this.linkEmbeddingFilter = entryFilter
this.embeddedLinks = true

@@ -46,0 +48,0 @@ return this

@@ -28,4 +28,9 @@ import LinkDictionary from '../model/LinkDictionary'

export default class LinkOperations {
static embedNodeLinks(node: object, dictionary: LinkDictionary) {
Object.entries(node).forEach(([key, value]) => {
static embedNodeLinks(node: object, dictionary: LinkDictionary, entryFilter?: (key: string, value: any) => boolean) {
const defaultFilter = () => true
const effectiveFilter = entryFilter ? entryFilter : defaultFilter
Object.entries(node)
.filter(([key, value]) => effectiveFilter(key, value))
.forEach(([key, value]) => {
// Two cases to handle: link object or array of links

@@ -32,0 +37,0 @@ if (typeof value === 'object') {

@@ -7,3 +7,2 @@ import * as amf from 'amf-client-js'

import JsonLd from "../src/JsonLd"
import CycleTrimmer from "../src/utils/CycleTrimmer"

@@ -68,7 +67,4 @@ const suites = [

const actual = AmfModelExpander.expand(JSON.parse(generated))
const replacer = (node: object) => { return node['@id'] }
const trimmer = new CycleTrimmer(replacer)
trimmer.trim(actual)
if (process.env.GOLDEN_OVERRIDE === 'true') {
fs.writeFileSync(goldenUrl, JsonLd.stringify(actual, false, false))
fs.writeFileSync(goldenUrl, JsonLd.stringify(actual, true))
}

@@ -75,0 +71,0 @@ const golden = JSON.parse(fs.readFileSync(goldenUrl, 'utf-8'))

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc