made with requirebin
Last active
August 29, 2015 14:06
-
-
Save BigBlueHat/d02da93e780833f0029c to your computer and use it in GitHub Desktop.
Hypothes.is JSON key removal tests / validator - requirebin sketch
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
// existence "tests" for | |
// https://github.com/hypothesis/h/issues/1207 | |
// using | |
// https://hypothes.is/api/annotations/KLUGtTMjTUSOPVvACd-dNA | |
// for reference. | |
var pointer = require('json-pointer'); | |
var obj = | |
{ | |
"text": "As in null?", | |
"updated": "2014-08-27T18:48:01.007982+00:00", | |
"id": "KLUGtTMjTUSOPVvACd-dNA", | |
"consumer": "00000000-0000-0000-0000-000000000000", | |
"permissions": { | |
"read": [ | |
"acct:BigBlueHat@hypothes.is", | |
"group:__world__" | |
], | |
"update": [ | |
"acct:BigBlueHat@hypothes.is" | |
], | |
"delete": [ | |
"acct:BigBlueHat@hypothes.is" | |
], | |
"admin": [ | |
"acct:BigBlueHat@hypothes.is" | |
] | |
}, | |
"uri": "http://scalar.usc.edu/dev/dev-test-book-cantaloupe/index", | |
"document": { | |
"prism": {}, | |
"dc": {}, | |
"reply_to": [], | |
"highwire": {}, | |
"eprints": {}, | |
"facebook": {}, | |
"title": "Home", | |
"twitter": {}, | |
"link": [ | |
{ | |
"href": "http://scalar.usc.edu/dev/dev-test-book-cantaloupe/index" | |
}, | |
{ | |
"href": "http://scalar.usc.edu/dev/dev-test-book-cantaloupe/index", | |
"type": "", | |
"rel": "canonical" | |
} | |
], | |
"favicon": "http://scalar.usc.edu/dev/system/application/views/arbors/html5_RDFa/favicon_16.gif" | |
}, | |
"target": [ | |
{ | |
"selector": [ | |
{ | |
"startOffset": 57, | |
"startContainer": "/article[1]/span[1]/div[1]/div[2]", | |
"type": "RangeSelector", | |
"endOffset": 62, | |
"endContainer": "/article[1]/span[1]/div[1]/div[2]" | |
}, | |
{ | |
"start": 517, | |
"type": "TextPositionSelector", | |
"end": 522 | |
}, | |
{ | |
"prefix": "t, consectetur adipiscing elit. ", | |
"suffix": " imperdiet lorem vitae felis gra", | |
"type": "TextQuoteSelector", | |
"exact": "Nulla" | |
} | |
], | |
"source": "http://scalar.usc.edu/dev/dev-test-book-cantaloupe/index", | |
"pos": { | |
"top": 212.3333282470703, | |
"height": 19 | |
}, | |
"quote": "Nulla", | |
"$$hashKey": "00P", | |
"showDiff": false | |
} | |
], | |
"tags": [ | |
"embed.js", | |
"scalar" | |
], | |
"reply_list": [], | |
"quote": "Nulla", | |
"created": "2014-08-26T14:06:52.563308+00:00", | |
"user": "acct:BigBlueHat@hypothes.is" | |
}; | |
var paths = [ | |
'/reply_list', | |
'/quote', | |
'/target/0/pos', | |
'/target/0/quote', | |
'/target/0/$$hashKey', | |
'/target/0/showDiff' | |
]; | |
// tests existence of "cruft" keys. Results should all be true. | |
for (var i=0; i < paths.length; i++) { | |
console.log(paths[i], "are you dead yet?!", !pointer.has(obj, paths[i])); | |
} |
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
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var hasOwn=Object.prototype.hasOwnProperty;var toString=Object.prototype.toString;module.exports=function forEach(obj,fn,ctx){if(toString.call(fn)!=="[object Function]"){throw new TypeError("iterator must be a function")}var l=obj.length;if(l===+l){for(var i=0;i<l;i++){fn.call(ctx,obj[i],i,obj)}}else{for(var k in obj){if(hasOwn.call(obj,k)){fn.call(ctx,obj[k],k,obj)}}}}},{}],"json-pointer":[function(require,module,exports){"use strict";var each=require("foreach");module.exports=api;function api(obj,pointer,value){if(arguments.length===3){return api.set(obj,pointer,value)}if(arguments.length===2){return api.get(obj,pointer)}var wrapped=api.bind(api,obj);for(var name in api){if(api.hasOwnProperty(name)){wrapped[name]=api[name].bind(wrapped,obj)}}return wrapped}api.get=function get(obj,pointer){var tok,refTokens=api.parse(pointer);while(refTokens.length){tok=refTokens.shift();if(!obj.hasOwnProperty(tok)){throw new Error("Invalid reference token: "+tok)}obj=obj[tok]}return obj};api.set=function set(obj,pointer,value){var refTokens=api.parse(pointer),tok,nextTok=refTokens[0];while(refTokens.length>1){tok=refTokens.shift();nextTok=refTokens[0];if(!obj.hasOwnProperty(tok)){if(nextTok.match(/^\d+$/)){obj[tok]=[]}else{obj[tok]={}}}obj=obj[tok]}obj[nextTok]=value;return this};api.remove=function(obj,pointer){var refTokens=api.parse(pointer);var finalToken=refTokens.pop();if(finalToken===undefined){throw new Error('Invalid JSON pointer for remove: "'+pointer+'"')}delete api.get(obj,api.compile(refTokens))[finalToken]};api.dict=function dict(obj,descend){var results={};api.walk(obj,function(value,pointer){results[pointer]=value},descend);return results};api.walk=function walk(obj,iterator,descend){var refTokens=[];descend=descend||function(value){var type=Object.prototype.toString.call(value);return type==="[object Object]"||type==="[object Array]"};(function next(cur){each(cur,function(value,key){refTokens.push(String(key));if(descend(value)){next(value)}else{iterator(value,api.compile(refTokens))}refTokens.pop()})})(obj)};api.has=function has(obj,pointer){try{api.get(obj,pointer)}catch(e){return false}return true};api.escape=function escape(str){return str.toString().replace(/~/g,"~0").replace(/\//g,"~1")};api.unescape=function unescape(str){return str.replace(/~1/g,"/").replace(/~0/g,"~")};api.parse=function parse(pointer){if(pointer===""){return[]}if(pointer.charAt(0)!=="/"){throw new Error("Invalid JSON pointer: "+pointer)}return pointer.substring(1).split(/\//).map(api.unescape)};api.compile=function compile(refTokens){if(refTokens.length===0){return""}return"/"+refTokens.map(api.escape).join("/")}},{foreach:1}]},{},[]);var pointer=require("json-pointer");var obj={text:"As in null?",updated:"2014-08-27T18:48:01.007982+00:00",id:"KLUGtTMjTUSOPVvACd-dNA",consumer:"00000000-0000-0000-0000-000000000000",permissions:{read:["acct:BigBlueHat@hypothes.is","group:__world__"],update:["acct:BigBlueHat@hypothes.is"],"delete":["acct:BigBlueHat@hypothes.is"],admin:["acct:BigBlueHat@hypothes.is"]},uri:"http://scalar.usc.edu/dev/dev-test-book-cantaloupe/index",document:{prism:{},dc:{},reply_to:[],highwire:{},eprints:{},facebook:{},title:"Home",twitter:{},link:[{href:"http://scalar.usc.edu/dev/dev-test-book-cantaloupe/index"},{href:"http://scalar.usc.edu/dev/dev-test-book-cantaloupe/index",type:"",rel:"canonical"}],favicon:"http://scalar.usc.edu/dev/system/application/views/arbors/html5_RDFa/favicon_16.gif"},target:[{selector:[{startOffset:57,startContainer:"/article[1]/span[1]/div[1]/div[2]",type:"RangeSelector",endOffset:62,endContainer:"/article[1]/span[1]/div[1]/div[2]"},{start:517,type:"TextPositionSelector",end:522},{prefix:"t, consectetur adipiscing elit. ",suffix:" imperdiet lorem vitae felis gra",type:"TextQuoteSelector",exact:"Nulla"}],source:"http://scalar.usc.edu/dev/dev-test-book-cantaloupe/index",pos:{top:212.3333282470703,height:19},quote:"Nulla",$$hashKey:"00P",showDiff:false}],tags:["embed.js","scalar"],reply_list:[],quote:"Nulla",created:"2014-08-26T14:06:52.563308+00:00",user:"acct:BigBlueHat@hypothes.is"};var paths=["/reply_list","/quote","/target/0/pos","/target/0/quote","/target/0/$$hashKey","/target/0/showDiff"];for(var i=0;i<paths.length;i++){console.log(paths[i],"are you dead yet?!",!pointer.has(obj,paths[i]))} |
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
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"json-pointer": "0.2.2" | |
} | |
} |
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
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; } | |
body, html { height: 100%; width: 100%; }</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment