@@ -115,6 +115,8 @@ Utils.compareIDs = function(ida, idb) {
115115 return false
116116}
117117
118+
119+
118120/**
119121 * Shortens a URL to its compressed format - returns the full URL if not possible
120122 */
@@ -145,6 +147,68 @@ Utils.unshorten = function(url) {
145147 return url
146148}
147149
150+
151+ /* shortens a jsonld document to its prefixed form */
152+ Utils . json_shorten = function ( jsonld , prefixes ) {
153+ const shorten_json_val = ( val , prefixes ) => {
154+ if ( Array . isArray ( val ) ) {
155+ let nvals = [ ]
156+ for ( var i = 0 ; i < val . length ; i ++ ) {
157+ nvals . push ( shorten_json_val ( val [ i ] , prefixes ) )
158+ }
159+ return nvals
160+ }
161+ else if ( typeof val == "object" ) {
162+ return this . json_shorten ( val , prefixes )
163+ }
164+ else if ( typeof val == "string" ) {
165+ return this . shorten ( val , prefixes )
166+ }
167+ else {
168+ return val
169+ }
170+ }
171+ prefixes = prefixes || jsonld [ "@context" ]
172+ let nujson = { }
173+ for ( var key in jsonld ) {
174+ let nkey = ( key [ 0 ] == "@" ? key : this . shorten ( key , prefixes ) )
175+ let nval = shorten_json_val ( jsonld [ key ] , prefixes )
176+ nujson [ nkey ] = nval
177+ }
178+ return nujson
179+ }
180+
181+ /* Unshortens a jsonld document to its full form */
182+ Utils . json_unshorten = function ( jsonld , prefixes ) {
183+ const unshorten_json_val = ( val , prefixes ) => {
184+ if ( Array . isArray ( val ) ) {
185+ let nvals = [ ]
186+ for ( var i = 0 ; i < val . length ; i ++ ) {
187+ nvals . push ( unshorten_json_val ( val [ i ] , prefixes ) )
188+ }
189+ return nvals
190+ }
191+ else if ( typeof val == "object" ) {
192+ return this . json_unshorten ( val , prefixes )
193+ }
194+ else if ( typeof val == "string" ) {
195+ return this . unshorten ( val , prefixes )
196+ }
197+ else {
198+ return val
199+ }
200+ }
201+ prefixes = prefixes || jsonld [ "@context" ]
202+ let nujson = { }
203+ for ( var key in jsonld ) {
204+ let nkey = ( key [ 0 ] == "@" ? key : this . unshorten ( key , prefixes ) )
205+ let nval = unshorten_json_val ( jsonld [ key ] , prefixes )
206+ nujson [ nkey ] = nval
207+ }
208+ return nujson
209+ }
210+
211+
148212/**
149213 * Tests a string to see if it is a valid URL -
150214 * Valid URLs are those that start with http:// or https://
0 commit comments