Skip to content

Commit

Permalink
only inline allocated vars
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Jun 29, 2020
1 parent adf40bd commit 3419563
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ var get = function(obj, additionalSchemas, ptr) {
}
}

var formatName = function(field) {
field = JSON.stringify(field)
var pattern = /\[([^\[\]"]+)\]/
while (pattern.test(field)) field = field.replace(pattern, '."+$1+"')
return field
}

var types = {}

types.any = function() {
Expand Down Expand Up @@ -123,8 +116,27 @@ var compile = function(schema, cache, root, reporter, opts) {
opts.greedy : false;

var syms = {}
var allocated = []
var gensym = function(name) {
return name+(syms[name] = (syms[name] || 0)+1)
var res = name+(syms[name] = (syms[name] || 0)+1)
allocated.push(res)
return res
}

var formatName = function(field) {
var s = JSON.stringify(field)
try {
var pattern = /\[([^\[\]"]+)\]/
while (pattern.test(s)) s = s.replace(pattern, replacer)
return s
} catch (_) {
return JSON.stringify(field)
}

function replacer (match, v) {
if (allocated.indexOf(v) === -1) throw new Error('Unreplaceable')
return '." + ' + v + ' + "'
}
}

var reversePatterns = {}
Expand All @@ -140,6 +152,7 @@ var compile = function(schema, cache, root, reporter, opts) {
var genloop = function() {
var v = vars.shift()
vars.push(v+v[0])
allocated.push(v)
return v
}

Expand Down

0 comments on commit 3419563

Please sign in to comment.