Skip to content

Commit

Permalink
Added helper and rules for validating a string is an html5 tag name.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrich committed May 28, 2024
1 parent 2dc14e2 commit c2f06bc
Show file tree
Hide file tree
Showing 10 changed files with 992 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {Ruleset} from '../src/ruleset';
value.must.havePropertyWithType('name', 'string'),
value.is.iterable(),
value.must.be.an.ipv4addr(),
value.is.type('array')
value.is.type('array'),
value.is.an.html5Tag()
);

console.debug(`Rules: ${ruleset.rules.length}`);
Expand Down
3 changes: 3 additions & 0 deletions src/block/an.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import {Block} from '../block';
import {type MatcherFactory} from '../matcher/factory';
import {matcherMkArray} from '../matcher/mk/array';
import {matcherMkHtml5Tag} from '../matcher/mk/html5/tag';
import {matcherMkInt} from '../matcher/mk/int';
import {matcherMkIpv4Addr} from '../matcher/mk/ipv4addr';
import {matcherMkIpv6Addr} from '../matcher/mk/ipv6addr';
Expand All @@ -41,6 +42,7 @@ export class BlockAn<InputT = unknown> extends Block<Statement<InputT>> {
public readonly int: MatcherFactory<InputT, never, BlockLink<InputT>>;
public readonly ipv4addr: MatcherFactory<InputT, never, BlockLink<InputT>>;
public readonly ipv6addr: MatcherFactory<InputT, never, BlockLink<InputT>>;
public readonly html5Tag: MatcherFactory<InputT, never, BlockLink<InputT>>;

constructor(init: BlockInit<InputT>) {
super(
Expand All @@ -56,5 +58,6 @@ export class BlockAn<InputT = unknown> extends Block<Statement<InputT>> {
this.int = matcherMkInt<InputT>(init);
this.ipv4addr = matcherMkIpv4Addr<InputT>(init);
this.ipv6addr = matcherMkIpv6Addr<InputT>(init);
this.html5Tag = matcherMkHtml5Tag<InputT>(init);
}
}
16 changes: 16 additions & 0 deletions src/block/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import {matcherMkBetween} from '../matcher/mk/between';
import {type Primitive} from '@toreda/types';
import {matcherMkIterable} from '../matcher/mk/iterable';
import {matcherMkTruthy} from '../matcher/mk/truthy';
import {BlockA} from './a';
import {BlockAn} from './an';

/**
* Matchers following 'is' in rule statements.
Expand Down Expand Up @@ -70,6 +72,9 @@ export class BlockIs<InputT = unknown> extends Block<Statement<InputT>> {
public readonly empty: MatcherFactory<InputT, unknown, BlockLink<InputT>>;
public readonly divisibleBy: MatcherFactory<InputT, number, BlockLink<InputT>>;
public readonly type: MatcherFactory<InputT, string, BlockLink<InputT>>;
public readonly an: BlockAn<InputT>;
public readonly a: BlockA<InputT>;

/**
* Determine if `value` supports iteration, but does check for a specific iterable type.
*/
Expand All @@ -92,6 +97,17 @@ export class BlockIs<InputT = unknown> extends Block<Statement<InputT>> {
init.stmt
);

this.an = new BlockAn<InputT>({
...init,
name: 'an',
tracer: this.tracer
});
this.a = new BlockA<InputT>({
...init,
name: 'a',
tracer: this.tracer
});

this.between = matcherMkBetween<InputT>({
...init,
tracer: this.tracer,
Expand Down
147 changes: 147 additions & 0 deletions src/html5/tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* MIT License
*
* Copyright (c) 2019 - 2024 Toreda, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

/**
* @category Validators – Html5
*/
export type Html5Tag =
| 'area'
| 'article'
| 'aside'
| 'audio'
| 'b'
| 'base'
| 'basefront'
| 'bdi'
| 'bdo'
| 'blockquote'
| 'body'
| 'br'
| 'button'
| 'canvas'
| 'caption'
| 'cite'
| 'code'
| 'col'
| 'colgroup'
| 'data'
| 'del'
| 'details'
| 'dfn'
| 'dialog'
| 'dir'
| 'div'
| 'dl'
| 'dt'
| 'em'
| 'embed'
| 'fieldset'
| 'figcaption'
| 'figure'
| 'font'
| 'footer'
| 'form'
| 'frame'
| 'frameset'
| 'h1'
| 'h2'
| 'h3'
| 'h4'
| 'h5'
| 'h6'
| 'head'
| 'header'
| 'hgroup'
| 'hr'
| 'html'
| 'i'
| 'iframe'
| 'img'
| 'input'
| 'ins'
| 'kbd'
| 'keygen'
| 'label'
| 'legend'
| 'li'
| 'link'
| 'main'
| 'map'
| 'mark'
| 'menu'
| 'menuitem'
| 'meta'
| 'meter'
| 'nav'
| 'noframes'
| 'noscript'
| 'object'
| 'ol'
| 'optgroup'
| 'option'
| 'output'
| 'p'
| 'param'
| 'picture'
| 'pre'
| 'progress'
| 'q'
| 'rp'
| 'rt'
| 'ruby'
| 's'
| 'samp'
| 'script'
| 'section'
| 'select'
| 'small'
| 'source'
| 'span'
| 'strike'
| 'strong'
| 'style'
| 'sub'
| 'summary'
| 'sup'
| 'svg'
| 'table'
| 'tbody'
| 'td'
| 'template'
| 'textarea'
| 'tfoot'
| 'th'
| 'thead'
| 'time'
| 'title'
| 'tr'
| 'track'
| 'tt'
| 'u'
| 'ul'
| 'var'
| 'video'
| 'wbr'
| string;
121 changes: 121 additions & 0 deletions src/html5/tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import {type Html5Tag} from './tag';

export const html5Tags: Set<Html5Tag> = new Set<Html5Tag>([
'area',
'article',
'aside',
'audio',
'b',
'base',
'basefront',
'bdi',
'bdo',
'blockquote',
'body',
'br',
'button',
'canvas',
'caption',
'cite',
'code',
'col',
'colgroup',
'data',
'del',
'details',
'dfn',
'dialog',
'dir',
'div',
'dl',
'dt',
'em',
'embed',
'fieldset',
'figcaption',
'figure',
'font',
'footer',
'form',
'frame',
'frameset',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'head',
'header',
'hgroup',
'hr',
'html',
'i',
'iframe',
'img',
'input',
'ins',
'kbd',
'keygen',
'label',
'legend',
'li',
'link',
'main',
'map',
'mark',
'menu',
'menuitem',
'meta',
'meter',
'nav',
'noframes',
'noscript',
'object',
'ol',
'optgroup',
'option',
'output',
'p',
'param',
'picture',
'pre',
'progress',
'q',
'rp',
'rt',
'ruby',
's',
'samp',
'script',
'section',
'select',
'small',
'source',
'span',
'strike',
'strong',
'style',
'sub',
'summary',
'sup',
'svg',
'table',
'tbody',
'td',
'template',
'textarea',
'tfoot',
'th',
'thead',
'time',
'title',
'tr',
'track',
'tt',
'u',
'ul',
'var',
'video',
'wbr'
]);
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export {ErrorContext} from './error/context';
export {ErrorContextData} from './error/context/data';
export {errorMkCode} from './error/mk/code';
export {greaterThan} from './greater/than';
export {Html5Tag} from './html5/tag';
export {html5Tags} from './html5/tags';
export {isArray} from './is/array';
export {isArrayEmpty} from './is/array/empty';
export {isArrayNotEmpty} from './is/array/not/empty';
Expand Down
44 changes: 44 additions & 0 deletions src/is/html5/tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* MIT License
*
* Copyright (c) 2019 - 2024 Toreda, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

import {type Html5Tag} from '../../html5/tag';
import {html5Tags} from '../../html5/tags';

/**
* Determine if value is a strict boolean type. Does not return true for
* otherwise truthy non-boolean values.
* @param value
* @returns
*
* @category Validators – Html5
*/
export function isHtml5Tag(value: unknown): value is Html5Tag {
if (typeof value !== 'string') {
return false;
}

const lower = value.toLocaleLowerCase().trim();
return html5Tags.has(lower);
}
Loading

0 comments on commit c2f06bc

Please sign in to comment.