functionalscript 0.0.340 → 0.0.343
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/node.js.yml +1 -1
- package/html/index.js +121 -0
- package/html/test.js +13 -0
- package/package.json +3 -3
- package/test.js +1 -0
package/html/index.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
const list = require('../types/list/index.js')
|
|
2
|
+
const object = require('../types/object/index.js')
|
|
3
|
+
const operator = require('../types/function/operator/index.js')
|
|
4
|
+
const { compose } = require('../types/function/index.js')
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {|
|
|
8
|
+
* 'a' |
|
|
9
|
+
* 'b' |
|
|
10
|
+
* 'body' |
|
|
11
|
+
* 'canvas' |
|
|
12
|
+
* 'div' |
|
|
13
|
+
* 'h1' |
|
|
14
|
+
* 'h2' |
|
|
15
|
+
* 'h3' |
|
|
16
|
+
* 'h4' |
|
|
17
|
+
* 'h5' |
|
|
18
|
+
* 'h6' |
|
|
19
|
+
* 'head' |
|
|
20
|
+
* 'html' |
|
|
21
|
+
* 'label' |
|
|
22
|
+
* 'p' |
|
|
23
|
+
* 'pre' |
|
|
24
|
+
* 'script' |
|
|
25
|
+
* 'table' |
|
|
26
|
+
* 'td' |
|
|
27
|
+
* 'th' |
|
|
28
|
+
* 'title' |
|
|
29
|
+
* 'tr'
|
|
30
|
+
* } Tag
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @typedef {|
|
|
35
|
+
* 'img' |
|
|
36
|
+
* 'input' |
|
|
37
|
+
* 'link' |
|
|
38
|
+
* 'meta'
|
|
39
|
+
* } ShortTag
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/** @typedef {readonly[ShortTag]} ShortElement1*/
|
|
43
|
+
|
|
44
|
+
/** @typedef {readonly[ShortTag, Attributes]} ShortElement2 */
|
|
45
|
+
|
|
46
|
+
/** @typedef {readonly[Tag, readonly Node[]]} Element2 */
|
|
47
|
+
|
|
48
|
+
/** @typedef {readonly[Tag, Attributes, Nodes]} Element3*/
|
|
49
|
+
|
|
50
|
+
/** @typedef {ShortElement1 | ShortElement2 | Element2 | Element3} Element */
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @typedef {{
|
|
54
|
+
* readonly[k in string]: string
|
|
55
|
+
* }} Attributes
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/** @typedef {list.List<Node>} Nodes */
|
|
59
|
+
|
|
60
|
+
/** @typedef {Element | string} Node */
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* https://stackoverflow.com/questions/7381974/which-characters-need-to-be-escaped-in-html
|
|
64
|
+
*
|
|
65
|
+
* @type {(code: number) => string}
|
|
66
|
+
*/
|
|
67
|
+
const escapeCharCode = code => {
|
|
68
|
+
switch (code) {
|
|
69
|
+
case 0x22: return '"'
|
|
70
|
+
case 0x26: return '&'
|
|
71
|
+
case 0x3C: return '<'
|
|
72
|
+
case 0x3E: return '>'
|
|
73
|
+
default: return String.fromCharCode(code)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const escape = compose(list.toCharCodes)(list.map(escapeCharCode))
|
|
78
|
+
|
|
79
|
+
/** @type {(n: Node) => list.List<string>} */
|
|
80
|
+
const node = n => typeof n === 'string' ? escape(n) : element(n)
|
|
81
|
+
|
|
82
|
+
const nodes = list.flatMap(node)
|
|
83
|
+
|
|
84
|
+
/** @type {(a: object.Entry<string>) => list.List<string>} */
|
|
85
|
+
const attribute = ([name, value]) => list.flat([[' ', name, '="'], escape(value), ['"']])
|
|
86
|
+
|
|
87
|
+
/** @type {(a: Attributes) => list.List<string>} */
|
|
88
|
+
const attributes = compose(Object.entries)(list.flatMap(attribute))
|
|
89
|
+
|
|
90
|
+
/** @type {(element: Element) => list.List<string>} */
|
|
91
|
+
const element = e => {
|
|
92
|
+
const f = () => {
|
|
93
|
+
switch (e.length) {
|
|
94
|
+
case 1: { return [[`<`, e[0], '/>']] }
|
|
95
|
+
case 2: {
|
|
96
|
+
const [tag, a] = e
|
|
97
|
+
return a instanceof Array ?
|
|
98
|
+
[['<', tag, '>'], nodes(a), ['</', tag, '>']] :
|
|
99
|
+
[[`<`, tag], attributes(a), [`/>`]]
|
|
100
|
+
}
|
|
101
|
+
default: {
|
|
102
|
+
const [tag, a, ns] = e
|
|
103
|
+
return [['<', tag], attributes(a), ['>'], nodes(ns), ['</', tag, '>']]
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return list.flat(f())
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const html = compose(element)(list.concat(['<!DOCTYPE html>']))
|
|
111
|
+
|
|
112
|
+
const htmlToString = compose(html)(list.fold(operator.concat)(''))
|
|
113
|
+
|
|
114
|
+
module.exports = {
|
|
115
|
+
/** @readonly */
|
|
116
|
+
element,
|
|
117
|
+
/** @readonly */
|
|
118
|
+
html,
|
|
119
|
+
/** @readonly */
|
|
120
|
+
htmlToString,
|
|
121
|
+
}
|
package/html/test.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const _ = require('./index.js')
|
|
2
|
+
|
|
3
|
+
{
|
|
4
|
+
const r = _.htmlToString(['html', []])
|
|
5
|
+
if (r !== '<!DOCTYPE html><html></html>') { throw r }
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
{
|
|
9
|
+
/** @type {_.Element} */
|
|
10
|
+
const x = ['div', {}, ['<div>&</div>', ['a', { href: 'hello"' }, []]]]
|
|
11
|
+
const s = _.htmlToString(x)
|
|
12
|
+
if (s !== '<!DOCTYPE html><div><div>&amp;</div><a href="hello""></a></div>') { throw s }
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "functionalscript",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.343",
|
|
4
4
|
"description": "FunctionalScript is a functional subset of JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://github.com/functionalscript/functionalscript#readme",
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@types/node": "^
|
|
32
|
-
"typescript": "^4.
|
|
31
|
+
"@types/node": "^17.0.35",
|
|
32
|
+
"typescript": "^4.7.2"
|
|
33
33
|
}
|
|
34
34
|
}
|
package/test.js
CHANGED
|
@@ -15,6 +15,7 @@ require('./types/function/compare/test.js')
|
|
|
15
15
|
require('./types/stringSet/test.js')
|
|
16
16
|
require('./commonjs/build/test.js')
|
|
17
17
|
require('./types/range/test.js')
|
|
18
|
+
require('./html/test.js')
|
|
18
19
|
|
|
19
20
|
/** @type {() => never} */
|
|
20
21
|
const assert = () => { throw 'assert' }
|