Skip to content

Commit dce657f

Browse files
committed
Add better docs to types
1 parent 632c933 commit dce657f

File tree

16 files changed

+389
-164
lines changed

16 files changed

+389
-164
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ coverage/
77
node_modules/
88
script/react-data.js
99
yarn.lock
10+
!/index.d.ts

index.d.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Info on a property.
3+
*/
4+
export interface Info {
5+
/**
6+
* Attribute name for the property that could be used in markup
7+
* (such as `'aria-describedby'`, `'allowfullscreen'`, `'xml:lang'`,
8+
* `'for'`, or `'charoff'`).
9+
*/
10+
attribute: string
11+
/**
12+
* The property is *like* a `boolean`
13+
* (such as `draggable`);
14+
* these properties have both an on and off state when defined,
15+
* *and* another state when not defined.
16+
*/
17+
booleanish: boolean
18+
/**
19+
* The property is a `boolean`
20+
* (such as `hidden`);
21+
* these properties have an on state when defined and an off state when not
22+
* defined.
23+
*/
24+
boolean: boolean
25+
/**
26+
* The property is a list separated by spaces or commas
27+
* (such as `strokeDashArray`).
28+
*/
29+
commaOrSpaceSeparated: boolean
30+
/**
31+
* The property is a list separated by commas
32+
* (such as `coords`).
33+
*/
34+
commaSeparated: boolean
35+
/**
36+
* The property is defined by a space;
37+
* this is the case for values in HTML
38+
* (including data and ARIA),
39+
* SVG, XML, XMLNS, and XLink;
40+
* not defined properties can only be found through `find`.
41+
*/
42+
defined: boolean
43+
/**
44+
* When working with the DOM,
45+
* this property has to be changed as a field on the element,
46+
* instead of through `setAttribute`
47+
* (this is true only for `'checked'`, `'multiple'`, `'muted'`, and
48+
* `'selected'`).
49+
*/
50+
mustUseProperty: boolean
51+
/**
52+
* The property is a `number` (such as `height`).
53+
*/
54+
number: boolean
55+
/**
56+
* The property is *like* a `boolean` (such as `download`);
57+
* these properties have an on state *and* more states when defined and an
58+
* off state when not defined.
59+
*/
60+
overloadedBoolean: boolean
61+
/**
62+
* JavaScript-style camel-cased name;
63+
* based on the DOM but sometimes different
64+
* (such as `'ariaDescribedBy'`, `'allowFullScreen'`, `'xmlLang'`,
65+
* `'htmlFor'`, `'charOff'`).
66+
*/
67+
property: string
68+
/**
69+
* The property is a list separated by spaces
70+
* (such as `className`).
71+
*/
72+
spaceSeparated: boolean
73+
/**
74+
* Space of the property.
75+
*/
76+
space: Space | null
77+
}
78+
79+
/**
80+
* Schema for a primary space.
81+
*/
82+
export interface Schema {
83+
/**
84+
* Object mapping normalized attributes and properties to properly cased
85+
* properties.
86+
*/
87+
normal: Record<string, string>
88+
/**
89+
* Object mapping properties to info.
90+
*/
91+
property: Record<string, Info>
92+
space: Space | null
93+
}
94+
95+
/**
96+
* Space of a property.
97+
*/
98+
export type Space = 'html' | 'svg' | 'xlink' | 'xmlns' | 'xml'
99+
100+
export {find} from './lib/find.js'
101+
102+
export {hastToReact} from './lib/hast-to-react.js'
103+
104+
/**
105+
* `Schema` for HTML,
106+
* with info on properties from HTML itself and related embedded spaces
107+
* (ARIA, XML, XMLNS, XLink).
108+
*/
109+
export const html: Schema
110+
111+
export {normalize} from './lib/normalize.js'
112+
113+
/**
114+
* `Schema` for SVG,
115+
* with info on properties from SVG itself and related embedded spaces
116+
* (ARIA, XML, XMLNS, XLink).
117+
*/
118+
export const svg: Schema

index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
/**
2-
* @typedef {import('./lib/util/info.js').Info} Info
3-
* @typedef {import('./lib/util/schema.js').Schema} Schema
4-
*/
5-
1+
// Note: types exposed from `index.d.ts`.
62
import {merge} from './lib/util/merge.js'
73
import {xlink} from './lib/xlink.js'
84
import {xml} from './lib/xml.js'

lib/find.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @typedef {import('./util/schema.js').Schema} Schema
2+
* @import {Schema} from '../index.js'
33
*/
44

55
import {normalize} from './normalize.js'
@@ -11,9 +11,34 @@ const dash = /-[a-z]/g
1111
const cap = /[A-Z]/g
1212

1313
/**
14+
* Look up info on a property.
15+
*
16+
* In most cases the given `schema` contains info on the property.
17+
* All standard,
18+
* most legacy,
19+
* and some non-standard properties are supported.
20+
* For these cases,
21+
* the returned `Info` has hints about the value of the property.
22+
*
23+
* `name` can also be a valid data attribute or property,
24+
* in which case an `Info` object with the correctly cased `attribute` and
25+
* `property` is returned.
26+
*
27+
* `name` can be an unknown attribute,
28+
* in which case an `Info` object with `attribute` and `property` set to the
29+
* given name is returned.
30+
* It is not recommended to provide unsupported legacy or recently specced
31+
* properties.
32+
*
33+
*
1434
* @param {Schema} schema
35+
* Schema;
36+
* either the `html` or `svg` export
1537
* @param {string} value
38+
* An attribute-like or property-like name;
39+
* it will be passed through `normalize` to hopefully find the correct info.
1640
* @returns {Info}
41+
* Info.
1742
*/
1843
export function find(schema, value) {
1944
const normal = normalize(value)

lib/hast-to-react.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/**
2-
* `hast` is close to `React`, but differs in a couple of cases.
2+
* Special cases for React (`Record<string, string>`).
33
*
4-
* To get a React property from a hast property, check if it is in
5-
* `hastToReact`, if it is, then use the corresponding value,
4+
* `hast` is close to `React` but differs in a couple of cases.
5+
* To get a React property from a hast property,
6+
* check if it is in `hastToReact`.
7+
* If it is, use the corresponding value;
68
* otherwise, use the hast property.
79
*
810
* @type {Record<string, string>}

lib/normalize.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/**
2+
* Get the cleaned case insensitive form of an attribute or property.
3+
*
24
* @param {string} value
5+
* An attribute-like or property-like name.
36
* @returns {string}
7+
* Value that can be used to look up the properly cased property on a
8+
* `Schema`.
49
*/
510
export function normalize(value) {
611
return value.toLowerCase()

lib/util/create.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/**
2-
* @typedef {import('./schema.js').Properties} Properties
3-
* @typedef {import('./schema.js').Normal} Normal
4-
*
5-
* @typedef {Record<string, string>} Attributes
6-
*
7-
* @typedef {Object} Definition
8-
* @property {Record<string, number|null>} properties
9-
* @property {(attributes: Attributes, property: string) => string} transform
10-
* @property {string} [space]
11-
* @property {Attributes} [attributes]
2+
* @import {Info, Space} from '../../index.js'
3+
*/
4+
5+
/**
6+
* @typedef Definition
7+
* @property {Record<string, string>} [attributes]
128
* @property {Array<string>} [mustUseProperty]
9+
* @property {Record<string, number | null>} properties
10+
* @property {Space} [space]
11+
* @property {(attributes: Record<string, string>, property: string) => string} transform
1312
*/
1413

1514
import {normalize} from '../normalize.js'
@@ -23,9 +22,9 @@ const own = {}.hasOwnProperty
2322
* @returns {Schema}
2423
*/
2524
export function create(definition) {
26-
/** @type {Properties} */
25+
/** @type {Record<string, Info>} */
2726
const property = {}
28-
/** @type {Normal} */
27+
/** @type {Record<string, string>} */
2928
const normal = {}
3029
/** @type {string} */
3130
let prop

lib/util/info.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1+
/**
2+
* @import {Info as InfoType} from '../../index.js'
3+
*/
4+
5+
/** @type {InfoType} */
16
export class Info {
27
/**
3-
* @constructor
48
* @param {string} property
59
* @param {string} attribute
610
*/
711
constructor(property, attribute) {
8-
/** @type {string} */
9-
this.property = property
10-
/** @type {string} */
1112
this.attribute = attribute
13+
this.property = property
1214
}
1315
}
1416

15-
/** @type {string|null} */
16-
Info.prototype.space = null
17-
Info.prototype.boolean = false
17+
Info.prototype.attribute = ''
1818
Info.prototype.booleanish = false
19-
Info.prototype.overloadedBoolean = false
20-
Info.prototype.number = false
21-
Info.prototype.commaSeparated = false
22-
Info.prototype.spaceSeparated = false
19+
Info.prototype.boolean = false
2320
Info.prototype.commaOrSpaceSeparated = false
24-
Info.prototype.mustUseProperty = false
21+
Info.prototype.commaSeparated = false
2522
Info.prototype.defined = false
23+
Info.prototype.mustUseProperty = false
24+
Info.prototype.number = false
25+
Info.prototype.overloadedBoolean = false
26+
Info.prototype.property = ''
27+
Info.prototype.spaceSeparated = false
28+
Info.prototype.space = null

lib/util/merge.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
/**
2-
* @typedef {import('./schema.js').Properties} Properties
3-
* @typedef {import('./schema.js').Normal} Normal
2+
* @import {Info, Space} from '../../index.js'
43
*/
54

65
import {Schema} from './schema.js'
76

87
/**
98
* @param {Schema[]} definitions
10-
* @param {string} [space]
9+
* @param {Space} [space]
1110
* @returns {Schema}
1211
*/
1312
export function merge(definitions, space) {
14-
/** @type {Properties} */
13+
/** @type {Record<string, Info>} */
1514
const property = {}
16-
/** @type {Normal} */
15+
/** @type {Record<string, string>} */
1716
const normal = {}
1817
let index = -1
1918

lib/util/schema.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
/**
2-
* @typedef {import('./info.js').Info} Info
3-
* @typedef {Record<string, Info>} Properties
4-
* @typedef {Record<string, string>} Normal
2+
* @import {Schema as SchemaType, Space} from '../../index.js'
53
*/
64

5+
/** @type {SchemaType} */
76
export class Schema {
87
/**
98
* @constructor
10-
* @param {Properties} property
11-
* @param {Normal} normal
12-
* @param {string} [space]
9+
* @param {SchemaType['property']} property
10+
* @param {SchemaType['normal']} normal
11+
* @param {Space | null | undefined} [space]
1312
*/
1413
constructor(property, normal, space) {
15-
this.property = property
1614
this.normal = normal
15+
this.property = property
16+
1717
if (space) {
1818
this.space = space
1919
}
2020
}
2121
}
2222

23-
/** @type {Properties} */
24-
Schema.prototype.property = {}
25-
/** @type {Normal} */
2623
Schema.prototype.normal = {}
27-
/** @type {string|null} */
24+
Schema.prototype.property = {}
2825
Schema.prototype.space = null

0 commit comments

Comments
 (0)