2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-06-17 19:21:20 +03:00

fix(#206): TagNode.create now with null content argument by default (#233)

* fix: TagNode.create with null content by default

* Create five-meals-sing.md

* fix: tests

* fix(preset): types inference

* fix: preset types

* fix: preset types

* fix: lock file, parser and utils

* refactor: move types to separate package

* fix(preset): add @bbob/core to dev deps

* fix(preset): lock file

* test(preset-vue): create tags

* test(preset-vue): tests

* chore(nx): fix nx cover deps

* chore: changesets
This commit is contained in:
Nikolay Kost
2024-06-24 01:32:15 +03:00
committed by GitHub
parent 95d9b8a2ba
commit 270f5645f8
58 changed files with 657 additions and 428 deletions
+3 -3
View File
@@ -1,3 +1,5 @@
import type { NodeContent, TagNodeObject, TagNodeTree } from "@bbob/types";
import { OPEN_BRAKET, CLOSE_BRAKET, SLASH } from './char';
import {
getUniqAttr,
@@ -8,8 +10,6 @@ import {
isTagNode,
} from './helpers';
import type { NodeContent, TagNodeObject, TagNodeTree } from "./types";
const getTagAttrs = <AttrValue>(tag: string, params: Record<string, AttrValue>) => {
const uniqAttr = getUniqAttr(params);
@@ -105,7 +105,7 @@ export class TagNode implements TagNodeObject {
return `${tagStart}${content}${this.toTagEnd({ openTag, closeTag })}`;
}
static create(tag: string, attrs: Record<string, unknown> = {}, content: TagNodeTree = []) {
static create(tag: string, attrs: Record<string, unknown> = {}, content: TagNodeTree = null) {
return new TagNode(tag, attrs, content)
}
+9 -8
View File
@@ -1,6 +1,7 @@
import type { NodeContent, StringNode } from "@bbob/types";
import { N } from './char';
import type { TagNode } from "./TagNode";
import type { NodeContent, StringNode } from "./types";
function isTagNode(el: unknown): el is TagNode {
return typeof el === 'object' && el !== null && 'tag' in el;
@@ -15,10 +16,10 @@ function isEOL(el: string) {
return el === N
}
function keysReduce<Res, Def extends Res, T extends Record<string, unknown>>(obj: T, reduce: (acc: Def, key: keyof T) => Res, def: Def): Res {
function keysReduce<Res, Def extends Res, T extends Record<string, unknown>>(obj: T, reduce: (acc: Def, key: keyof T, obj: T) => Res, def: Def): Res {
const keys = Object.keys(obj)
return keys.reduce((acc, key) => reduce(acc, key), def)
return keys.reduce((acc, key) => reduce(acc, key, obj), def)
}
function getNodeLength(node: NodeContent): number {
@@ -85,7 +86,7 @@ function attrValue<AttrValue = unknown>(name: string, value: AttrValue) {
* @example
* attrsToString({ 'foo': true, 'bar': bar' }) => 'foo="true" bar="bar"'
*/
function attrsToString<AttrValue = unknown>(values: Record<string, AttrValue> | null) {
function attrsToString<AttrValue = unknown>(values?: Record<string, AttrValue> | null) {
// To avoid some malformed attributes
if (values == null) {
return '';
@@ -93,7 +94,7 @@ function attrsToString<AttrValue = unknown>(values: Record<string, AttrValue> |
return keysReduce(
values,
(arr, key) => [...arr, attrValue(key, values[key])],
(arr, key, obj) => [...arr, attrValue(key, obj[key])],
[''],
).join(' ');
}
@@ -103,10 +104,10 @@ function attrsToString<AttrValue = unknown>(values: Record<string, AttrValue> |
* @example
* getUniqAttr({ 'foo': true, 'bar': bar' }) => 'bar'
*/
function getUniqAttr<Value>(attrs: Record<string, Value>) {
function getUniqAttr<Value>(attrs?: Record<string, Value>) {
return keysReduce(
attrs,
(res, key) => (attrs[key] === key ? attrs[key] : null),
attrs || {},
(res, key, obj) => (obj[key] === key ? obj[key] : null),
null,
)
}
-1
View File
@@ -1,4 +1,3 @@
export * from "./helpers";
export * from "./char";
export * from "./TagNode";
export * from "./types";
-13
View File
@@ -1,13 +0,0 @@
export type StringNode = string | number
export interface TagNodeObject {
readonly tag: string
attrs: Record<string, unknown>
content: TagNodeTree
}
export type NodeContent = TagNodeObject | StringNode | null
export type PartialNodeContent = Partial<TagNodeObject> | StringNode | null
export type TagNodeTree = NodeContent | NodeContent[] | null