Published
Edited
Jul 11, 2018
27 stars
Insert cell
Insert cell
{
const q = s => '`' + JSON.stringify(s) + '`'
const bq = s => { const j = JSON.stringify(s); return '`'+j.substr(1, j.length - 2).replace(/\\"/, '"')+'`' }
const match = (re, s) => { const m = re.exec(s); return m && m[0] }
let re1 = /[\\]./
let re2 = /\\./
const headers = ['input', bq('/[\\]./'), bq('/\\./')];
let mkd = headers.join('|') + "\n" + headers.map(s => '-').join('|') + "\n";
['abc\\\'123', "abc\\\"123", "abc"].forEach(s => {
mkd += [q(s), q(match(re1, s)), q(match(re2, s))].join('|') + "\n"
})
return md`${mkd}`
}
Insert cell
Insert cell
{
const tbl = (...header) => { return header.join('|') + "\n" + header.map(s => '-').join('|') + "\n" }
const mkd = tbl('input', bq("/[^\\']/"), bq('/[^\\"]/')) +
["a", "\\", "'", '"'].map(s => [q(s), q(match(/[^\\']/, s)), q(match(/[^\\"]/, s))].join('|')).join("\n")
return md`${mkd}`
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
escDblQuotes = s => s.replace(/\\?"/g, '\\"')
Insert cell
Insert cell
getString = s => {
const match = /'(?:\\.|[^\\'])*'|"(?:\\.|[^\\"])*"/.exec(s)
let m = match && match[0]
if (m && m[0] === "'") {
m = `"${m.substr(1, m.length - 2).replace(/\\?"/g, '\\"').replace(/\\'/g, "'")}"`
}
return m
}
Insert cell
Insert cell
Insert cell
identRe = /[$_A-Za-z][$_A-Za-z0-9]*/
Insert cell
Insert cell
Insert cell
{
const a = { foo: { bar: 'baz' }}
const b = { foo: ['baz'] }
return [a .foo .bar, a ['foo'] ['bar'], b ['foo'] [0]]
}
Insert cell
Insert cell
Insert cell
readSingleKey = s => {
const match = singleKeyRe.exec(s)
if (match && match.index == 0) {
let m = match[1] || match[2]
if (match[0][0] === '[') {
if (m[0] === "'") {
m = `"${m.substr(1, m.length - 2).replace(/\\?"/g, '\\"').replace(/\\'/g, "'")}"`
}
try {
m = JSON.parse(m)
} catch (e) {
m = undefined
}
}
return m
}
}
Insert cell
Insert cell
Insert cell
readKeys = s => {
const re = /\[\s*('(?:\\.|[^\\'])*'|"(?:\\.|[^\\"])*"|\d+)\s*\]|\.([$_A-Za-z][$_A-Za-z0-9]*)|\s+/g
let nextIndex = 0
let keys = []
let match
while (match = re.exec(s)) {
if (match.index !== nextIndex) {
break
}
let m = match[1] || match[2]
if (match[0][0] === '[') {
if (m[0] === "'") {
m = `"${m.substr(1, m.length - 2).replace(/\\?"/g, '\\"').replace(/\\'/g, "'")}"`
}
try {
m = JSON.parse(m)
} catch (e) {
m = undefined
}
}
if (m && match[0][0] === '.' || match[0][0] === '[') {
keys.push(m)
}
nextIndex = match.index + match[0].length
}
return keys
}
Insert cell
Insert cell
Insert cell
// index.js
function j2ref(str, startIndex = 0, matchStart = true) {
const identRe = /\s*([$_A-Za-z][$_A-Za-z0-9]*)/g
if (startIndex !== 0) {
identRe.lastIndex = startIndex
}
let identMatch = identRe.exec(str)
if (! (identMatch && identMatch[0])) {
return
}
if (matchStart && identMatch.index !== startIndex) {
return
}
const re = /\[\s*('(?:\\.|[^\\'])*'|"(?:\\.|[^\\"])*"|\d+)\s*\]|\.([$_A-Za-z][$_A-Za-z0-9]*)|\s+/g
let nextIndex = identMatch.index + identMatch[0].length
re.lastIndex = nextIndex
let keys = [identMatch[1]]
let match
while (match = re.exec(str)) {
if (match.index !== nextIndex) {
break
}
let m = match[1] || match[2]
if (match[0][0] === '[') {
if (m[0] === "'") {
m = `"${m.substr(1, m.length - 2).replace(/\\?"/g, '\\"').replace(/\\'/g, "'")}"`
}
try {
m = JSON.parse(m)
} catch (e) {
m = undefined
}
}
if (m && match[0][0] === '.' || match[0][0] === '[') {
keys.push(m)
}
nextIndex = match.index + match[0].length
}
return {
keys,
index: identMatch.index,
matched: str.substr(identMatch.index, nextIndex - identMatch.index)
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more