@@ -0,0 +1,365 @@
10+From [Joe Bryan](https://github.com/joemfb/nock.js/blob/master/nock.js):
13+(function (self, factory) {
16+ if (typeof define === 'function' && define.amd) {
18+ } else if (typeof module === 'object' && typeof module.exports === 'object') {
19+ module.exports = factory()
21+ self.nock = factory()
27+ * Nock is a combinator interpreter on nouns. A noun is an atom or a cell.
28+ * An atom is an unsigned integer of any size; a cell is an ordered pair of nouns.
30+ * @see http://urbit.org/docs/nock/definition/
31+ * @see http://media.urbit.org/whitepaper.pdf
34+ var useMacros = false
40+ * `s` is a subject noun,
41+ * `f` is a formula (or cell of formulas)
47+ * wut (?): test for atom (1) or cell (0)
53+ return typeof n === 'number' ? 1 : 0
57+ * lus (+): increment an atom
63+ if (wut(n) === 0) throw new Error('lus cell')
68+ * tis (=): test equality
75+ if (wut(n) === 1) throw new Error('tis atom')
76+ return deepEqual(n[0], n[1]) ? 0 : 1
80+ * fas (/): resolve a tree address
85+ * /[(a + a) b] /[2 /[a b]]
86+ * /[(a + a + 1) b] /[3 /[a b]]
89+ function fas (addr, n) {
90+ if (n === undefined) throw new Error('invalid fas noun')
91+ if (addr === 0) throw new Error('invalid fas addr: 0')
93+ if (addr === 1) return n
94+ if (addr === 2) return n[0]
95+ if (addr === 3) return n[1]
97+ return fas(2 + (addr % 2), fas((addr / 2) | 0, n))
103+ * slot (0): resolve a tree address
107+ function slot (s, f) {
110+ if (p === undefined) throw new Error('invalid fas addr: ' + f)
116+ * constant (1): return the formula regardless of subject
120+ function constant (s, f) {
125+ * evaluate (2): evaluate the product of second formula against the product of the first
127+ * *[a 2 b c] *[*[a b] *[a c]]
129+ function evaluate (s, f) {
130+ return nock(nock(s, f[0]), nock(s, f[1]))
134+ * cell (3): test if the product is a cell
138+ function cell (s, f) {
139+ return wut(nock(s, f))
143+ * incr (4): increment the product
147+ function incr (s, f) {
148+ return lus(nock(s, f))
152+ * eq (5): test for equality between nouns in the product
156+ function eq (s, f) {
157+ return tis(nock(s, f))
160+ /* macro-formulas */
163+ * ife (6): if/then/else
165+ * *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
167+ function macroIfe (s, f) {
168+ return nock(s, [2, [[0, 1], [2, [[1, [f[1][0], f[1][1]]], [[1, 0], [2, [[1, [2, 3]], [[1, 0], [4, [4, f[0]]]]]]]]]]])
171+ function ife (s, f) {
172+ var cond = nock(s, f[0])
174+ if (cond === 0) return nock(s, f[1][0])
175+ if (cond === 1) return nock(s, f[1][1])
177+ throw new Error('invalid ife conditional')
181+ * compose (7): evaluate formulas composed left-to-right
183+ * *[a 7 b c] *[a 2 b 1 c]
185+ function macroCompose (s, f) {
186+ return nock(s, [2, [f[0], [1, f[1]]]])
189+ function compose (s, f) {
191+ // return nock(nock(s, f[0]), constant(s, f[1]))
192+ return nock(nock(s, f[0]), f[1])
196+ * extend (8): evaluate the second formula against [product of first, subject]
198+ * *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
200+ function macroExtend (s, f) {
201+ return nock(s, [7, [[[7, [[0, 1], f[0]]], [0, 1]], f[1]]])
204+ function extend (s, f) {
206+ // return nock([compose(s, [[0, 1], f[0]]), s], f[1])
207+ return nock([nock(s, f[0]), s], f[1])
211+ * invoke (9): construct a core and evaluate one of it's arms against it
213+ * *[a 9 b c] *[a 7 c 2 [0 1] 0 b]
215+ function macroInvoke (s, f) {
216+ return nock(s, [7, [f[1], [2, [[0, 1], [0, f[0]]]]]])
219+ function invoke (s, f) {
220+ var prod = nock(s, f[1])
221+ return nock(prod, slot(prod, f[0]))
225+ * hint (10): skip first formula, evaluate second
227+ * *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
228+ * *[a 10 b c] *[a c]
230+ function macroHint (s, f) {
231+ if (wut(f[0]) === 0) return nock(s, [8, [f[0][1], [7, [[0, 3], f[1]]]]])
232+ return nock(s, f[1])
235+ function hint (s, f) {
236+ if (wut(f[0]) === 0) {
237+ if (wut(f[0][1]) === 1) throw new Error('invalid hint')
240+ return nock(s, f[1])
243+ /* indexed formula functions */
244+ var macroFormulas = [slot, constant, evaluate, cell, incr, eq, macroIfe, macroCompose, macroExtend, macroInvoke, macroHint]
245+ var formulas = [slot, constant, evaluate, cell, incr, eq, ife, compose, extend, invoke, hint]
252+ * *[a [b c] d] [*[a b c] *[a d]]
255+ function nock (s, f) {
256+ if (wut(f[0]) === 0) return [nock(s, f[0]), nock(s, f[1])]
260+ if (idx > 10) throw new Error('invalid formula: ' + idx)
262+ if (useMacros) return macroFormulas[idx](s, f[1])
264+ return formulas[idx](s, f[1])
267+ /* construct a JS noun (group an array into pairs, associating right) */
268+ function assoc (x) {
269+ if (!x.length) return x
271+ if (x.length === 1) return assoc(x[0])
273+ return [assoc(x[0]), assoc(x.slice(1))]
276+ /* deep equality for arrays or primitives */
277+ function deepEqual (a, b) {
278+ if (a === b) return true
280+ if (!(Array.isArray(a) && Array.isArray(b))) return false
281+ if (a.length !== b.length) return false
283+ for (var i = 0; i < a.length; i++) {
284+ if (!deepEqual(a[i], b[i])) return false
290+ /* parse a hoon-serialized nock formula and construct a JS noun */
291+ function parseNoun (x) {
292+ if (Array.isArray(x)) return assoc(x)
294+ if (typeof x === 'string') {
295+ var str = x.replace(/[\."']/g, '').split(' ').join(',')
296+ return assoc(JSON.parse(str))
302+ function nockInterface () {
303+ var args = [].slice.call(arguments)
304+ var subject, formula, noun
306+ if (args.length === 1) {
307+ formula = parseNoun(args[0])
308+ } else if (args.length === 2) {
309+ subject = parseNoun(args[0])
310+ formula = parseNoun(args[1])
317+ if (!formula) throw new Error('formula required')
324+ return nock(subject, formula)
328+ nock: nockInterface,
330+ useMacros: function (arg) {
331+ useMacros = arg === undefined || arg
336+ parseNoun: parseNoun,
337+ deepEqual: deepEqual
354+ macroCompose: macroCompose,
356+ macroExtend: macroExtend,
358+ macroInvoke: macroInvoke,
360+ macroHint: macroHint,