Skip to content

Commit

Permalink
remove utilities that are no longer needed
Browse files Browse the repository at this point in the history
  • Loading branch information
skanaar committed Dec 1, 2024
1 parent 8b8ae7e commit b6debb0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 121 deletions.
51 changes: 11 additions & 40 deletions dist/nomnoml.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,22 @@
summa += transform(list[i]);
return summa;
}
function find(list, predicate) {
for (let i = 0; i < list.length; i++)
if (predicate(list[i]))
return list[i];
return undefined;
}
function last(list) {
return list[list.length - 1];
}
function hasSubstring(haystack, needle) {
if (needle === '')
return true;
if (!haystack)
return false;
return haystack.indexOf(needle) !== -1;
}
function indexBy(list, key) {
const obj = {};
for (let i = 0; i < list.length; i++)
obj[list[i][key]] = list[i];
return obj;
}
function uniqueBy(list, property) {
const seen = {};
const out = [];
for (let i = 0; i < list.length; i++) {
const key = list[i][property];
if (!seen[key]) {
seen[key] = true;
out.push(list[i]);
}
}
return out;
}

var util = /*#__PURE__*/Object.freeze({
__proto__: null,
find: find,
hasSubstring: hasSubstring,
indexBy: indexBy,
last: last,
range: range,
sum: sum,
uniqueBy: uniqueBy
sum: sum
});

function buildStyle(conf, title, body = {}) {
Expand Down Expand Up @@ -944,24 +916,23 @@
return 'network-simplex';
}
function parseCustomStyle(styleDef) {
const contains = hasSubstring;
const floatingKeywords = styleDef.replace(/[a-z]*=[^ ]+/g, '');
const titleDef = last(styleDef.match('title=([^ ]*)') || ['']);
const bodyDef = last(styleDef.match('body=([^ ]*)') || ['']);
return {
title: {
bold: contains(titleDef, 'bold') || contains(floatingKeywords, 'bold'),
underline: contains(titleDef, 'underline') || contains(floatingKeywords, 'underline'),
italic: contains(titleDef, 'italic') || contains(floatingKeywords, 'italic'),
center: !(contains(titleDef, 'left') || contains(styleDef, 'align=left')),
bold: titleDef.includes('bold') || floatingKeywords.includes('bold'),
underline: titleDef.includes('underline') || floatingKeywords.includes('underline'),
italic: titleDef.includes('italic') || floatingKeywords.includes('italic'),
center: !(titleDef.includes('left') || styleDef.includes('align=left')),
},
body: {
bold: contains(bodyDef, 'bold'),
underline: contains(bodyDef, 'underline'),
italic: contains(bodyDef, 'italic'),
center: contains(bodyDef, 'center'),
bold: bodyDef.includes('bold'),
underline: bodyDef.includes('underline'),
italic: bodyDef.includes('italic'),
center: bodyDef.includes('center'),
},
dashed: contains(styleDef, 'dashed'),
dashed: styleDef.includes('dashed'),
fill: last(styleDef.match('fill=([^ ]*)') || []),
stroke: last(styleDef.match('stroke=([^ ]*)') || []),
visual: (last(styleDef.match('visual=([^ ]*)') || []) || 'class'),
Expand Down Expand Up @@ -1226,7 +1197,7 @@
renderLabel(r.startLabel);
renderLabel(r.endLabel);
if (r.type !== '-/-') {
if (hasSubstring(r.type, '--')) {
if (r.type.includes('--')) {
const dash = Math.max(4, 2 * config.lineWidth);
g.save();
g.setLineDash([dash, dash]);
Expand Down
21 changes: 10 additions & 11 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Ranker } from 'graphre/decl/types'
import { Config, Style, Visual } from './domain'
import { linearParse } from './linearParse'
import { hasSubstring, last } from './util'
import { last } from './util'
import { styles } from './visuals'

export { ParseError } from './linearParse'
Expand Down Expand Up @@ -58,24 +58,23 @@ export function parse(source: string): ParsedDiagram {
}

function parseCustomStyle(styleDef: string): Style {
const contains = hasSubstring
const floatingKeywords = styleDef.replace(/[a-z]*=[^ ]+/g, '')
const titleDef = last(styleDef.match('title=([^ ]*)') || [''])
const bodyDef = last(styleDef.match('body=([^ ]*)') || [''])
return {
title: {
bold: contains(titleDef, 'bold') || contains(floatingKeywords, 'bold'),
underline: contains(titleDef, 'underline') || contains(floatingKeywords, 'underline'),
italic: contains(titleDef, 'italic') || contains(floatingKeywords, 'italic'),
center: !(contains(titleDef, 'left') || contains(styleDef, 'align=left')),
bold: titleDef.includes('bold') || floatingKeywords.includes('bold'),
underline: titleDef.includes('underline') || floatingKeywords.includes('underline'),
italic: titleDef.includes('italic') || floatingKeywords.includes('italic'),
center: !(titleDef.includes('left') || styleDef.includes('align=left')),
},
body: {
bold: contains(bodyDef, 'bold'),
underline: contains(bodyDef, 'underline'),
italic: contains(bodyDef, 'italic'),
center: contains(bodyDef, 'center'),
bold: bodyDef.includes('bold'),
underline: bodyDef.includes('underline'),
italic: bodyDef.includes('italic'),
center: bodyDef.includes('center'),
},
dashed: contains(styleDef, 'dashed'),
dashed: styleDef.includes('dashed'),
fill: last(styleDef.match('fill=([^ ]*)') || []),
stroke: last(styleDef.match('stroke=([^ ]*)') || []),
visual: (last(styleDef.match('visual=([^ ]*)') || []) || 'class') as Visual,
Expand Down
4 changes: 2 additions & 2 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Config, RelationLabel, TextStyle } from './domain'
import { Graphics } from './Graphics'
import { LayoutedAssoc, LayoutedNode, LayoutedPart } from './layouter'
import { drawTerminators, getPath } from './terminators'
import { hasSubstring, last } from './util'
import { last } from './util'
import { add, Vec } from './vector'
import { buildStyle, styles, visualizers } from './visuals'

Expand Down Expand Up @@ -123,7 +123,7 @@ export function render(graphics: Graphics, config: Config, compartment: Layouted
renderLabel(r.endLabel)

if (r.type !== '-/-') {
if (hasSubstring(r.type, '--')) {
if (r.type.includes('--')) {
const dash = Math.max(4, 2 * config.lineWidth)
g.save()
g.setLineDash([dash, dash])
Expand Down
21 changes: 0 additions & 21 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,11 @@ export function sum<T>(list: ArrayLike<T>, transform: (item: T) => number) {
for (let i = 0, len = list.length; i < len; i++) summa += transform(list[i])
return summa
}
export function find<T>(list: T[], predicate: (e: T) => boolean) {
for (let i = 0; i < list.length; i++) if (predicate(list[i])) return list[i]
return undefined
}
export function last<T>(list: T[]) {
return list[list.length - 1]
}
export function hasSubstring(haystack: string, needle: string) {
if (needle === '') return true
if (!haystack) return false
return haystack.indexOf(needle) !== -1
}
export function indexBy<T>(list: T[], key: keyof T): { [key: string]: T } {
const obj: { [key: string]: T } = {}
for (let i = 0; i < list.length; i++) obj[list[i][key] as any] = list[i]
return obj
}
export function uniqueBy<T>(list: T[], property: keyof T): T[] {
const seen: { [key: string]: boolean } = {}
const out: T[] = []
for (let i = 0; i < list.length; i++) {
const key = list[i][property] as unknown as string
if (!seen[key]) {
seen[key] = true
out.push(list[i])
}
}
return out
}
29 changes: 0 additions & 29 deletions test/test.misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,42 +59,13 @@ test('util sum', function () {
21
)
})
test('util find', function () {
assert(
skanaar.find([{ a: 0 }, { a: 4, needle: true }, { a: 17 }], (e) => e.a == 4),
'=',
{ a: 4, needle: true }
)
})
test('util last', function () {
assert(skanaar.last([{ a: 0 }, { a: 4 }, { a: 17, needle: true }]), '=', { a: 17, needle: true })
})
test('util hasSubstring', function () {
assert(skanaar.hasSubstring('xyz abc', 'xyz'), '=', true)
assert(skanaar.hasSubstring('1 xyz 0', 'xyz'), '=', true)
assert(skanaar.hasSubstring('abc xyz', 'xyz'), '=', true)
})
test('util indexBy', function () {
assert(skanaar.indexBy([], 'name'), '=', {})
assert(skanaar.indexBy([{ name: 'apa' }], 'name'), '=', { apa: { name: 'apa' } })
})
test('util uniqueBy', function () {
assert(
skanaar.uniqueBy(
[
{ a: 4, b: 'x' },
{ a: 17, c: 'y' },
{ a: 4, d: 'z' },
],
'a'
),
'=',
[
{ a: 4, b: 'x' },
{ a: 17, c: 'y' },
]
)
})

test('processImports resolves shallow imports', function () {
var mockFiles = {
Expand Down
40 changes: 22 additions & 18 deletions webapp/FileSystem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Observable } from "./Observable"
import { Route } from "./Route"
import { find } from "../src/util"
import { Observable } from './Observable'
import { Route } from './Route'

export interface FileEntry {
name: string
Expand Down Expand Up @@ -38,7 +37,8 @@ export class FileSystem {
var route = Route.from(path)
this.storage = this.routedStorage(route)
var index = await this.storage.files()
this.activeFile = find(index, e => e.name === route.path) || fileEntry(route.path, 'local_file')
this.activeFile =
index.find((e) => e.name === route.path) ?? fileEntry(route.path, 'local_file')
this.signals.trigger('updated')
}

Expand All @@ -56,12 +56,12 @@ export class FileSystem {
type StoreKind = 'local_default' | 'local_file' | 'filesystem' | 'url'

function fileEntry(name: string, backingStore: StoreKind): FileEntry {
return { date: (new Date()).toISOString(), name, backingStore }
return { date: new Date().toISOString(), name, backingStore }
}

interface GraphStore {
files(): Promise<FileEntry[]>
read(): Promise<string|undefined>
read(): Promise<string | undefined>
insert(src: string): Promise<void>
save(src: string): Promise<void>
clear(): Promise<void>
Expand All @@ -74,12 +74,14 @@ export class StoreDefaultBuffer implements GraphStore {
async files(): Promise<FileEntry[]> {
return JSON.parse(localStorage['nomnoml.file_index'] || '[]') as FileEntry[]
}
async read(): Promise<string|undefined> { return localStorage[this.storageKey] }
async insert(source: string): Promise<void> { }
async read(): Promise<string | undefined> {
return localStorage[this.storageKey]
}
async insert(source: string): Promise<void> {}
async save(source: string): Promise<void> {
localStorage[this.storageKey] = source
}
async clear(): Promise<void> { }
async clear(): Promise<void> {}
}

export class StoreUrl implements GraphStore {
Expand All @@ -88,10 +90,12 @@ export class StoreUrl implements GraphStore {
async files(): Promise<FileEntry[]> {
return JSON.parse(localStorage['nomnoml.file_index'] || '[]') as FileEntry[]
}
async read(): Promise<string|undefined> { return this.source }
async insert(source: string): Promise<void> { }
async save(source: string): Promise<void> { }
async clear(): Promise<void> { }
async read(): Promise<string | undefined> {
return this.source
}
async insert(source: string): Promise<void> {}
async save(source: string): Promise<void> {}
async clear(): Promise<void> {}
}

export class StoreLocal implements GraphStore {
Expand All @@ -103,15 +107,15 @@ export class StoreLocal implements GraphStore {
async files(): Promise<FileEntry[]> {
return JSON.parse(localStorage['nomnoml.file_index'] || '[]') as FileEntry[]
}
async read(): Promise<string|undefined> {
async read(): Promise<string | undefined> {
return localStorage[this.storageKey]
}
async insert(source: string): Promise<void> {
var entry: FileEntry = fileEntry(this.name, 'local_file')
var index = await this.files()
if (!find(index, e => e.name === this.name)) {
if (!index.find((e) => e.name === this.name)) {
index.push(entry)
index.sort((a,b) => a.name.localeCompare(b.name))
index.sort((a, b) => a.name.localeCompare(b.name))
localStorage['nomnoml.file_index'] = JSON.stringify(index)
}
localStorage[this.storageKey] = source
Expand All @@ -122,7 +126,7 @@ export class StoreLocal implements GraphStore {
async clear(): Promise<void> {
localStorage.removeItem(this.storageKey)
var files = await this.files()
var index = files.filter(e => e.name != this.name)
var index = files.filter((e) => e.name != this.name)
localStorage['nomnoml.file_index'] = JSON.stringify(index)
}
}
}

0 comments on commit b6debb0

Please sign in to comment.