Skip to content

Commit

Permalink
fix: Bug in SAFARI prevents topsheets from loading
Browse files Browse the repository at this point in the history
Safari cannot parse RegEx with "lookbehind", bug is >3 years old
https://stackoverflow.com/questions/51568821/works-in-chrome-but-breaks-in-safari-invalid-regular-expression-invalid-group

Worked around it.
  • Loading branch information
billyc committed Feb 4, 2022
1 parent 13fd4d6 commit e1b4907
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
11 changes: 2 additions & 9 deletions src/components/TopSheet/TopSheet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,11 @@ export type TableRow = {
@Component({ components: {} })
export default class VueComponent extends Vue {
@Prop({ required: true })
private fileSystemConfig!: FileSystemConfig
@Prop({ required: true })
private subfolder!: string
@Prop({ required: true }) private fileSystemConfig!: FileSystemConfig
@Prop({ required: true }) private subfolder!: string
@Prop({ required: true }) private files!: string[]
@Prop({ required: true }) private yaml!: string
@Prop({ required: true }) private allConfigFiles!: YamlConfigs
@Prop({ required: false }) private cardId?: string
private solverThread!: any
Expand Down
41 changes: 28 additions & 13 deletions src/components/TopSheet/TopSheetWorker.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ async function runTopSheet(props: {

// set up user entry boxes if first run
if (!Object.keys(_boxValues).length) {
console.log('** resetting boxvalues')
// console.log('** resetting boxvalues')
_boxValues = getBoxValues(_yaml)
}

Expand Down Expand Up @@ -189,7 +189,7 @@ function buildOutputs() {
}

function getBoxValues(yaml: TopsheetYaml) {
console.log('getBoxValues')
// console.log('getBoxValues')
const boxes = yaml.userEntries
if (!boxes) return {}

Expand Down Expand Up @@ -232,7 +232,7 @@ function doAllCalculations() {
// solve the equation using nerdamer
const value = nerdamer(expr).valueOf()
calculations[calc] = value
console.log(calc, value)
// console.log(calc, value)
} catch (e) {
calculations[calc] = `Error:${calc}: ${expr}`
}
Expand All @@ -244,15 +244,17 @@ function doAllCalculations() {
function getFilterReplacements(calc: string): any[] {
const expr = '' + _yaml.calculations[calc]

// this regex matches @filter(file.column==value)
const re = /(?<=\@filter\().*?(?=\))/g
const patterns = expr.match(re)
// this regex matches @filter(file.column==value):

if (!patterns) return []
if (patterns.length > 1) throw Error('Only one filter allowed per calculation')
// OOPS: SAFARI doesn't support real regex, fuck you safari!
// const re = /(?<=\@filter\().*?(?=\))/g
// const patterns = expr.match(re)

const loc = expr.indexOf('@filter(')
if (loc == -1) return []

// analyze first @filter only
const innerPattern = patterns[0]
const innerPattern = expr.substring(loc + 8, expr.indexOf(')', loc))

const filters = {
'==': (row: any) => row[column] == value,
Expand Down Expand Up @@ -290,9 +292,22 @@ function getFilterReplacements(calc: string): any[] {

function getFileVariableReplacements(expr: string) {
// this regex matches {variables}
const re = /(?<={).*?(?=})/g
const patterns = expr.match(re)
if (!patterns) return expr
// OOPS! SAFARI FUCKALL DOESN'T SUPPORT REGEX WITH LOOKBEHIND
// broken: const re = /(?<={).*?(?=})/g
// const patterns = expr.match(re)

// non-regex version because SAFARI IS THE WORST :-O
let offset = 0
const patterns: string[] = []

while (expr.indexOf('{', offset) > -1) {
offset = 1 + expr.indexOf('{', offset)
const element = expr.substring(offset, expr.indexOf('}', offset))
patterns.push(element)
}

// no vars? just return the string
if (!patterns.length) return expr

// for each {variable}, do a lookup and replace
for (const p of patterns) {
Expand Down Expand Up @@ -350,7 +365,7 @@ async function loadFiles() {
await parseVariousFileTypes(inputFile, filename, text)
} catch (e) {
console.error(e)
throw e
// throw e
}
}
}
Expand Down

0 comments on commit e1b4907

Please sign in to comment.