-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (159 loc) · 4.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
export function untab (ss, ...vs) {
let r = ''
ss.forEach((s, i) => {
r += `${s}${i === ss.length - 1 ? '' : vs[i]}`
})
return r.replace(/>\s+</g, '><').trim()
}
const html = untab // syntax highlighting
class MethodTrap {
constructor () {
const handlers = {
get (target, prop, receiver) {
if (Reflect.has(target, prop)) return Reflect.get(target, prop, receiver)
else {
return function (...args) {
return Reflect.get(target, 'methodTrap').call(receiver, prop, ...args)
}
}
},
}
return new Proxy(this, handlers)
}
methodTrap (name) {
throw new Error(`Method ${name} not implemented`)
}
}
export class Item extends MethodTrap {
i
constructor (item) {
super()
if (typeof item !== 'object' || Array.isArray(item)) throw new Error('item must be an object')
this.i = item
}
attrs (dict = this.i) {
return Object.entries(dict)
.filter(([_k, v]) => v !== undefined)
.map(([k, v]) => `${k}="${v instanceof Date ? this.presentDate(v) : v}"`)
.join(' ')
}
link (href, text, target) {
return html`<a ${this.attrs({ href, target })}>${text}</a>`
}
dataAttrs (dict = this.i) {
return this.attrs(
Object.fromEntries(
Object.entries(dict).map(([k, v]) =>
[`data-${k}`, v instanceof Date ? this.presentDate(v) : v]
),
),
)
}
presentDate (date) {
return date.toLocaleDateString()
}
}
export class List extends MethodTrap {
items
constructor (items) {
super()
if (!Array.isArray(items)) throw new Error('items must be an array')
if (items.length > 0 && typeof items[0] !== 'string') { throw new Error('items must be an array of strings') }
this.items = items
}
}
export class Collection extends MethodTrap {
entities
entityKeys
constructor (entities, keys) {
super()
if (!Array.isArray(entities)) throw new Error('collection must be an array')
if (entities.length > 0 && typeof entities[0] !== 'object') { throw new Error('collection must be an array of objects') }
this.entities = entities
this.entityKeys = keys || entities.length > 0 ? Object.keys(entities[0]) : []
}
methodTrap (name, ...args) {
console.log(`${name} called with`, args)
return ''
}
#list (templateFn, tag) {
return html`
<${tag}>
${this.entities.map((item) => `<li>${templateFn(item)}</li>`).join('')}
</${tag}>
`
}
/**
* @param {string} key
* @returns {string}
*/
ul (key) {
return this.#list((item) => item[key], 'ul')
}
/**
* @param {string} key
* @returns {string}
*/
ol (key) {
return this.#list((item) => item[key], 'ol')
}
/** @returns {string} HTML table */
table () {
const keys = this.entityKeys
return html`
<table>
<thead>
<tr>
${keys.map((key) => `<th>${key}</th>`).join('')}
</tr>
</thead>
<tbody>
${this.entities
.map(
(item) => `
<tr>
${keys.map((key) => `<td>${item[key]}</td>`).join('')}
</tr>
`,
)
.join('')}
</tbody>
</table>
`
}
/**
* @param {string[] | function} keysOrFn
* @param {string} separator
* @returns {string}
*/
list (keysOrFn, separator = ' ') {
let templateFn
if (typeof keysOrFn === 'function') {
templateFn = keysOrFn
} else {
templateFn = (item) => keysOrFn.map((key) => item[key]).join(separator)
}
return this.#list(templateFn, 'ul')
}
/**
* experimental series renderer
* @param {string} tag tag name like 'div' or 'span'
* @param {(item: Item) => string} tagFn opening tag content template function
* @param {(item: Item) => string} templateFn tag content template function
* @returns {string} HTML content
*/
render (tag, tagFn, templateFn) {
return html`
${this.entities
.map((i) => {
const item = new Item(i)
return html`
<${tag} ${tagFn(item)}>
${templateFn(item)}
</${tag}>
`
})
.join('')}
`
}
}