tsprose 0.0.1

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/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ export * from './default/mix.js';
2
+ export * from './default/text.js';
3
+ export * from './utils/hash.js';
4
+ export * from './children.js';
5
+ export * from './document.js';
6
+ export * from './element.js';
7
+ export * from './elementUtils.js';
8
+ export * from './error.js';
9
+ export * from './id.js';
10
+ export * from './json.js';
11
+ export * from './rawToProse.js';
12
+ export * from './schema.js';
13
+ export * from './storage.js';
14
+ export * from './tag.js';
15
+ export * from './tagUtils.js';
16
+ export * from './unique.js';
17
+ export * from './walk.js';
package/dist/json.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function toJSON(value: unknown, indent?: number): string;
2
+ export declare function fromJSON(json: string): any;
package/dist/json.js ADDED
@@ -0,0 +1,46 @@
1
+ import { PROSE_ELEMENT_PREFIX, RAW_ELEMENT_PREFIX } from './element.js';
2
+ import { SCHEMA_PREFIX } from './schema.js';
3
+ function encodeSchema(schema) {
4
+ return (schema.name +
5
+ (schema.type === 'block' ? '0' : '1') +
6
+ (schema.linkable === false ? '0' : schema.linkable === 'always' ? '2' : '1'));
7
+ }
8
+ function decodeSchema(encoded) {
9
+ const name = encoded.slice(0, -2);
10
+ const typeFlag = encoded.at(-2);
11
+ const linkFlag = encoded.at(-1);
12
+ return {
13
+ [SCHEMA_PREFIX]: true,
14
+ name,
15
+ type: typeFlag === '0' ? 'block' : 'inliner',
16
+ linkable: linkFlag === '0' ? false : linkFlag === '2' ? 'always' : true,
17
+ };
18
+ }
19
+ function isElement(obj) {
20
+ return obj[RAW_ELEMENT_PREFIX] === true || obj[PROSE_ELEMENT_PREFIX] === true;
21
+ }
22
+ export function toJSON(value, indent) {
23
+ return JSON.stringify(value, function (key, val) {
24
+ if (!isElement(this))
25
+ return val;
26
+ if (key === RAW_ELEMENT_PREFIX || key === PROSE_ELEMENT_PREFIX) {
27
+ const schema = this.schema;
28
+ return encodeSchema(schema);
29
+ }
30
+ if (key === 'schema') {
31
+ return undefined;
32
+ }
33
+ return val;
34
+ }, indent);
35
+ }
36
+ export function fromJSON(json) {
37
+ return JSON.parse(json, function (key, val) {
38
+ if ((key === RAW_ELEMENT_PREFIX || key === PROSE_ELEMENT_PREFIX) &&
39
+ typeof val === 'string') {
40
+ const schema = decodeSchema(val);
41
+ this.schema = schema;
42
+ return true;
43
+ }
44
+ return val;
45
+ });
46
+ }
@@ -0,0 +1 @@
1
+ export * from './jsx-runtime.js';
@@ -0,0 +1 @@
1
+ export * from './jsx-runtime.js';
@@ -0,0 +1,25 @@
1
+ import { mixSchema } from './default/mix.js';
2
+ import type { RawElement, ToRawElement } from './element.js';
3
+ declare global {
4
+ namespace JSX {
5
+ /** No intrinsic elements to avoid first letter casing confusion. */
6
+ interface IntrinsicElements {
7
+ }
8
+ /**
9
+ * Unfortunately, JSX/TSX tag constructions (<...>) always return general JSX.Element type.
10
+ * No matter what tag you use, it will be widened to JSX.Element loosing all generics.
11
+ * This prevents some cool compile/editor time checkings like prohibiting certain tags in specific contexts (blocks in inliners and etc.)
12
+ * All this has to be done in runtime or with other tools, like ESLint.
13
+ *
14
+ * @todo Remove this when TypeScript supports proper JSX/TSX generics.
15
+ * @see [TypeScript issue](https://github.com/microsoft/TypeScript/issues/21699)
16
+ */
17
+ type Element = RawElement;
18
+ }
19
+ }
20
+ export declare function Fragment(props: {
21
+ children: {};
22
+ }): ToRawElement<typeof mixSchema>;
23
+ export declare function jsx(tag: any, props: any): any;
24
+ export declare const jsxs: typeof jsx;
25
+ export declare const jsxDEV: typeof jsx;
@@ -0,0 +1,24 @@
1
+ import { normalizeChildren } from './children.js';
2
+ import { mixSchema } from './default/mix.js';
3
+ import { makeRawElement } from './elementUtils.js';
4
+ import { TSProseError } from './error.js';
5
+ export function Fragment(props) {
6
+ const children = normalizeChildren(props?.children);
7
+ if (!children) {
8
+ throw new TSProseError(`JSX Fragment must have at least one child!`);
9
+ }
10
+ return makeRawElement({
11
+ schema: mixSchema,
12
+ elementHandler: (element) => {
13
+ element.children = children;
14
+ },
15
+ });
16
+ }
17
+ export function jsx(tag, props) {
18
+ if (tag === Fragment) {
19
+ return Fragment(props);
20
+ }
21
+ return tag(props);
22
+ }
23
+ export const jsxs = jsx;
24
+ export const jsxDEV = jsx;
@@ -0,0 +1,19 @@
1
+ import type { LinkableProseElement, ProseElement, RawElement } from './element.js';
2
+ import { type IdMaker } from './id.js';
3
+ export type RawToProseStep = (elements: {
4
+ rawElement: RawElement;
5
+ proseElement: ProseElement;
6
+ }) => void | Promise<void>;
7
+ export interface RawToProseResult {
8
+ prose: ProseElement;
9
+ takenIds: Set<string>;
10
+ uniques: Record<string, LinkableProseElement>;
11
+ }
12
+ export declare function rawToProse(args: {
13
+ rawProse: RawElement;
14
+ takenIds?: Set<string>;
15
+ pre?: (rawElement: RawElement) => void | Promise<void>;
16
+ post?: (proseElement: ProseElement) => void | Promise<void>;
17
+ step?: RawToProseStep;
18
+ idMaker?: IdMaker;
19
+ }): Promise<RawToProseResult>;
@@ -0,0 +1,59 @@
1
+ import { makeProseElement } from './elementUtils.js';
2
+ import { TSProseError } from './error.js';
3
+ import { defaultIdMaker } from './id.js';
4
+ import { isWalkStop, walkPost } from './walk.js';
5
+ export async function rawToProse(args) {
6
+ const { rawProse, pre, post, step } = args;
7
+ const idMaker = args.idMaker || defaultIdMaker;
8
+ const takenIds = new Set(args.takenIds);
9
+ const uniques = {};
10
+ const prose = await walkPost(rawProse, async (rawElement, children) => {
11
+ if (pre) {
12
+ await pre(rawElement);
13
+ }
14
+ const proseElement = makeProseElement({
15
+ schema: rawElement.schema,
16
+ });
17
+ if (rawElement.data) {
18
+ proseElement.data = rawElement.data;
19
+ }
20
+ if (rawElement.storageKey) {
21
+ proseElement.storageKey = rawElement.storageKey;
22
+ }
23
+ if (children) {
24
+ proseElement.children = children;
25
+ }
26
+ if (rawElement.schema.linkable) {
27
+ const elementId = idMaker(rawElement, takenIds);
28
+ if (takenIds.has(elementId)) {
29
+ throw new TSProseError(`Element ID collision: "${elementId}" is already taken!\nMake sure "idMaker" you are using generates non-repeating IDs!`);
30
+ }
31
+ proseElement.id = elementId;
32
+ takenIds.add(elementId);
33
+ if (rawElement.uniqueName) {
34
+ if (uniques[rawElement.uniqueName]) {
35
+ throw new TSProseError(`Duplicate uniqueName: "${rawElement.uniqueName}" is already used by another element!\nIf you are using document prose, make sure not to directly insert imported or manually created external uniques as they might intersect with document uniques!`);
36
+ }
37
+ uniques[rawElement.uniqueName] = proseElement;
38
+ }
39
+ }
40
+ if (post) {
41
+ await post(proseElement);
42
+ }
43
+ if (step) {
44
+ await step({
45
+ rawElement,
46
+ proseElement,
47
+ });
48
+ }
49
+ return proseElement;
50
+ });
51
+ if (isWalkStop(prose)) {
52
+ throw new TSProseError('Raw to Prose walk cannot be manually stopped!');
53
+ }
54
+ return {
55
+ prose,
56
+ takenIds,
57
+ uniques,
58
+ };
59
+ }
@@ -0,0 +1,25 @@
1
+ export declare const SCHEMA_PREFIX = "__TSPROSE_schema";
2
+ export type SchemaName = string;
3
+ export type SchemaType = 'block' | 'inliner';
4
+ export type SchemaLinkable = boolean | 'always';
5
+ export interface Schema {
6
+ [SCHEMA_PREFIX]: true;
7
+ name: SchemaName;
8
+ type: SchemaType;
9
+ linkable: SchemaLinkable;
10
+ Data: any;
11
+ Storage: any;
12
+ Children: Schema[] | undefined;
13
+ }
14
+ export interface BlockSchema extends Schema {
15
+ type: 'block';
16
+ }
17
+ export interface InlinerSchema extends Schema {
18
+ type: 'inliner';
19
+ Children: InlinerSchema[] | undefined;
20
+ }
21
+ export interface LinkableSchema extends Schema {
22
+ linkable: true | 'always';
23
+ }
24
+ export type RuntimeSchema<T extends Schema> = Omit<T, 'Data' | 'Storage' | 'Children'>;
25
+ export declare function defineSchema<T extends Schema>(runtimeSchema: Omit<RuntimeSchema<T>, typeof SCHEMA_PREFIX>): T;
package/dist/schema.js ADDED
@@ -0,0 +1,7 @@
1
+ export const SCHEMA_PREFIX = '__TSPROSE_schema';
2
+ export function defineSchema(runtimeSchema) {
3
+ return {
4
+ ...runtimeSchema,
5
+ [SCHEMA_PREFIX]: true,
6
+ };
7
+ }
@@ -0,0 +1,18 @@
1
+ import type { ProseElement, ToProseElement } from './element.js';
2
+ import type { Schema } from './schema.js';
3
+ export type ProseStorage = Record<string, any>;
4
+ export interface ProseWithStorage {
5
+ prose: ProseElement;
6
+ storage: ProseStorage;
7
+ }
8
+ export type ElementStorageCreator<TSchema extends Schema> = TSchema['Storage'] extends undefined ? never : (element: ToProseElement<TSchema>) => null | TSchema['Storage'] | Promise<null | TSchema['Storage']>;
9
+ export declare function fillProseStorage(args: {
10
+ prose: ProseElement;
11
+ storageCreators: Record<string, ElementStorageCreator<any>>;
12
+ alterValue?: (args: {
13
+ element: ToProseElement<Schema>;
14
+ storageKey: string;
15
+ value: any;
16
+ }) => any | Promise<any>;
17
+ storage?: ProseStorage;
18
+ }): Promise<ProseStorage>;
@@ -0,0 +1,30 @@
1
+ import { walkPre } from './walk.js';
2
+ export async function fillProseStorage(args) {
3
+ const storage = args.storage || {};
4
+ await walkPre(args.prose, async (element) => {
5
+ const storageKey = element.storageKey;
6
+ // Element is not supposed to have storage
7
+ if (!storageKey) {
8
+ return;
9
+ }
10
+ // Storage was already filled
11
+ if (storage[storageKey] !== undefined) {
12
+ return;
13
+ }
14
+ const storageCreator = args.storageCreators[element.schema.name];
15
+ let value = null;
16
+ const deUndefy = (value) => (value === undefined ? null : value);
17
+ if (storageCreator) {
18
+ value = deUndefy(await storageCreator(element));
19
+ }
20
+ if (args.alterValue) {
21
+ value = deUndefy(await args.alterValue({
22
+ element,
23
+ storageKey,
24
+ value,
25
+ }));
26
+ }
27
+ storage[storageKey] = value;
28
+ });
29
+ return storage;
30
+ }
package/dist/tag.d.ts ADDED
@@ -0,0 +1,45 @@
1
+ import { type NormalizedChildren } from './children.js';
2
+ import type { ToRawElement } from './element.js';
3
+ import type { LinkableSchema, Schema } from './schema.js';
4
+ import { type AutoUnique, type ToUnique } from './unique.js';
5
+ export declare const TAG_PREFIX = "__TSPROSE_tag";
6
+ export type ToTag<TSchema extends Schema, TagName extends string, Props extends Record<string, any>> = {
7
+ [TAG_PREFIX]: true;
8
+ tagName: TagName;
9
+ schema: TSchema;
10
+ } & ((props: TagProps<TSchema, TagName, Props>) => ToRawElement<TSchema, TagName>);
11
+ export type Tag = ToTag<Schema, string, any>;
12
+ export type LinkableTag = ToTag<LinkableSchema, string, any>;
13
+ export type TagProps<TSchema extends Schema, TagName extends string, Props extends Record<string, any> = {}> = TagChildren<TSchema, Props> & TagLinkable<TSchema, TagName> & Props;
14
+ export type TagChildren<TSchema extends Schema, Props> = 'children' extends keyof Props ? {} : [TSchema['Children']] extends [undefined] ? {
15
+ children?: never;
16
+ } : undefined extends TSchema['Children'] ? {
17
+ children?: {};
18
+ } : {
19
+ children: {};
20
+ };
21
+ export type TagLinkable<TSchema extends Schema, TagName extends string> = TSchema extends LinkableSchema ? TSchema['linkable'] extends 'always' ? {
22
+ $: ToUnique<ToTag<TSchema, TagName, any>> | AutoUnique;
23
+ } : TSchema['linkable'] extends true ? {
24
+ $?: ToUnique<ToTag<TSchema, TagName, any>> | AutoUnique;
25
+ } : {} : {};
26
+ export type ValidateTagCustomProps<Props extends Record<string, any> = {}> = Extract<keyof Props, '$'> extends infer Conflict ? [Conflict] extends [never] ? Props : `Forbidden custom tag property '${Extract<Conflict, string | number | bigint>}'!` : never;
27
+ export type NoChildren = {
28
+ children?: never;
29
+ };
30
+ export type OptionalChildren = {
31
+ children?: {};
32
+ };
33
+ export type RequiredChildren = {
34
+ children: {};
35
+ };
36
+ export declare function defineTag<TSchema extends Schema, TagName extends string>(tagParameters: {
37
+ tagName: TagName;
38
+ schema: TSchema;
39
+ }): <const Props extends ValidateTagCustomProps<Props>>(tagElementHandler: TagElementHandler<TagName, TSchema, Props>) => ToTag<TSchema, TagName, Props>;
40
+ export type TagElementHandler<TagName extends string, TSchema extends Schema, Props extends Record<string, any>> = (context: {
41
+ tagName: TagName;
42
+ props: TagProps<TSchema, TagName, Props>;
43
+ children: NormalizedChildren;
44
+ element: ToRawElement<TSchema, TagName>;
45
+ }) => void;
package/dist/tag.js ADDED
@@ -0,0 +1,53 @@
1
+ import { normalizeChildren } from './children.js';
2
+ import { makeRawElement } from './elementUtils.js';
3
+ import { TSProseError } from './error.js';
4
+ import { isAutoUnique } from './unique.js';
5
+ export const TAG_PREFIX = '__TSPROSE_tag';
6
+ export function defineTag(tagParameters) {
7
+ const tagName = tagParameters.tagName;
8
+ if (!tagName.trim()) {
9
+ throw new TSProseError(`Invalid tag name "${tagName}"!`);
10
+ }
11
+ function finalizeTag(tagElementHandler) {
12
+ const tag = (props) => {
13
+ const { tagName, schema } = tagParameters;
14
+ const children = normalizeChildren(props.children, (child) => {
15
+ if (schema.type === 'inliner' && child.schema.type === 'block') {
16
+ throw new TSProseError(`Inliner <${tagName}> cannot have block children! Detected block child "${child.schema.name}"!`);
17
+ }
18
+ });
19
+ const rawElement = makeRawElement({
20
+ tagName,
21
+ schema,
22
+ elementHandler: (element) => {
23
+ if (props.$) {
24
+ element.uniqueName = props.$.name;
25
+ props.$.rawElement = element;
26
+ if (isAutoUnique(props.$)) {
27
+ props.$.tag = tag;
28
+ }
29
+ }
30
+ else {
31
+ if (schema.linkable === 'always') {
32
+ throw new TSProseError(`Tag <${tagName}> requires a unique provided via "$" prop!`);
33
+ }
34
+ }
35
+ tagElementHandler({
36
+ tagName,
37
+ props,
38
+ children,
39
+ element,
40
+ });
41
+ },
42
+ });
43
+ return rawElement;
44
+ };
45
+ Object.defineProperties(tag, {
46
+ [TAG_PREFIX]: { value: true },
47
+ tagName: { value: tagName },
48
+ schema: { value: tagParameters.schema },
49
+ });
50
+ return tag;
51
+ }
52
+ return finalizeTag;
53
+ }
@@ -0,0 +1,12 @@
1
+ import type { NormalizedChildren } from './children.js';
2
+ import { TSProseError } from './error.js';
3
+ import type { BlockRawElement, InlinerRawElement, ToRawElement } from './element.js';
4
+ import type { Schema } from './schema.js';
5
+ export declare function createTagError(tagName: string, message: string): TSProseError;
6
+ export declare function ensureTagNoChildren(tagName: string, children: NormalizedChildren): asserts children is undefined;
7
+ export declare function ensureTagSingleChild<TSchema extends Schema, TagName extends string>(tagName: TagName, children: NormalizedChildren, schema?: TSchema): asserts children is [ToRawElement<TSchema, TagName>];
8
+ export declare function ensureTagSingleBlockChild(tagName: string, children: NormalizedChildren): asserts children is [BlockRawElement];
9
+ export declare function ensureTagSingleInlinerChild(tagName: string, children: NormalizedChildren): asserts children is [InlinerRawElement];
10
+ export declare function ensureTagChildren<TagName extends string, Schemas extends Schema | Schema[]>(tagName: TagName, children: NormalizedChildren, schemas?: Schemas): asserts children is (Schemas extends Schema[] ? ToRawElement<Schemas[number], TagName> : Schemas extends Schema ? ToRawElement<Schemas, TagName> : never)[];
11
+ export declare function ensureTagBlockChildren(tagName: string, children: NormalizedChildren): asserts children is BlockRawElement[];
12
+ export declare function ensureTagInlinerChildren(tagName: string, children: NormalizedChildren): asserts children is InlinerRawElement[];
@@ -0,0 +1,62 @@
1
+ import { TSProseError } from './error.js';
2
+ import { ensureRawElement } from './elementUtils.js';
3
+ export function createTagError(tagName, message) {
4
+ return new TSProseError(`<${tagName}> error!\n${message}`);
5
+ }
6
+ export function ensureTagNoChildren(tagName, children) {
7
+ if (children) {
8
+ throw createTagError(tagName, 'This tag cannot have children!');
9
+ }
10
+ }
11
+ export function ensureTagSingleChild(tagName, children, schema) {
12
+ if (!children || children.length !== 1) {
13
+ throw createTagError(tagName, 'This tag requires exactly one child element!');
14
+ }
15
+ const child = children[0];
16
+ ensureRawElement(child, schema);
17
+ }
18
+ export function ensureTagSingleBlockChild(tagName, children) {
19
+ ensureTagSingleChild(tagName, children);
20
+ if (children[0].schema.type !== 'block') {
21
+ throw createTagError(tagName, `This tag requires exactly one block child element but received "${children[0].schema.name}"!`);
22
+ }
23
+ }
24
+ export function ensureTagSingleInlinerChild(tagName, children) {
25
+ ensureTagSingleChild(tagName, children);
26
+ if (children[0].schema.type !== 'inliner') {
27
+ throw createTagError(tagName, `This tag requires exactly one inliner child element but received "${children[0].schema.name}"!`);
28
+ }
29
+ }
30
+ export function ensureTagChildren(tagName, children, schemas) {
31
+ if (!children) {
32
+ throw createTagError(tagName, 'This tag requires child elements!');
33
+ }
34
+ if (schemas) {
35
+ const validSchemas = new Set(schemas
36
+ ? Array.isArray(schemas)
37
+ ? schemas.map((s) => s.name)
38
+ : [schemas.name]
39
+ : undefined);
40
+ for (const child of children) {
41
+ if (!validSchemas.has(child.schema.name)) {
42
+ throw createTagError(tagName, `This tag cannot have "${child.schema.name}" child!`);
43
+ }
44
+ }
45
+ }
46
+ }
47
+ export function ensureTagBlockChildren(tagName, children) {
48
+ ensureTagChildren(tagName, children);
49
+ for (const child of children) {
50
+ if (child.schema.type !== 'block') {
51
+ throw createTagError(tagName, `This tag cannot have "${child.schema.name}" inliner child!`);
52
+ }
53
+ }
54
+ }
55
+ export function ensureTagInlinerChildren(tagName, children) {
56
+ ensureTagChildren(tagName, children);
57
+ for (const child of children) {
58
+ if (child.schema.type !== 'inliner') {
59
+ throw createTagError(tagName, `This tag cannot have "${child.schema.name}" block child!`);
60
+ }
61
+ }
62
+ }
@@ -0,0 +1,25 @@
1
+ import type { ToRawElement } from './element.js';
2
+ import type { LinkableTag } from './tag.js';
3
+ export declare const UNIQUE_PREFIX = "__TSPROSE_unique";
4
+ export interface ToUnique<Tag extends LinkableTag> {
5
+ [UNIQUE_PREFIX]: true;
6
+ documentId: string;
7
+ name: string;
8
+ tag: Tag;
9
+ rawElement: ToRawElement<Tag['schema'], Tag['tagName']>;
10
+ }
11
+ export type Unique = ToUnique<LinkableTag>;
12
+ export interface AutoUnique extends Unique {
13
+ auto: true;
14
+ }
15
+ export declare function defineUnique<Tag extends LinkableTag>(uniqueParameters: {
16
+ documentId: string;
17
+ name: string;
18
+ tag: Tag;
19
+ }): ToUnique<Tag>;
20
+ export declare function defineAutoUnique(uniqueParameters: {
21
+ documentId: string;
22
+ name: string;
23
+ }): AutoUnique;
24
+ export declare function isUnique<Tag extends LinkableTag>(unique: any, tag?: Tag): unique is ToUnique<Tag>;
25
+ export declare function isAutoUnique(unique: any): unique is AutoUnique;
package/dist/unique.js ADDED
@@ -0,0 +1,81 @@
1
+ import { TSProseError } from './error.js';
2
+ export const UNIQUE_PREFIX = '__TSPROSE_unique';
3
+ export function defineUnique(uniqueParameters) {
4
+ const { documentId, name, tag } = uniqueParameters;
5
+ const schema = tag.schema;
6
+ if (!schema.linkable) {
7
+ throw new TSProseError(`Unable to create unique "${name}" for <${tag.tagName}> because its schema is not linkable!`);
8
+ }
9
+ let rawElement;
10
+ return {
11
+ [UNIQUE_PREFIX]: true,
12
+ documentId,
13
+ name,
14
+ tag,
15
+ get rawElement() {
16
+ if (!rawElement) {
17
+ throw new TSProseError(`Unique "${name}" raw element is not assigned yet!`);
18
+ }
19
+ return rawElement;
20
+ },
21
+ set rawElement(value) {
22
+ if (rawElement) {
23
+ throw new TSProseError(`Unique "${name}" raw element is already assigned and cannot be reassigned!`);
24
+ }
25
+ if (value.tagName !== tag.tagName) {
26
+ throw new TSProseError(`Unique "${name}" requires <${tag.tagName}> element but received <${value.tagName}>!`);
27
+ }
28
+ rawElement = value;
29
+ },
30
+ };
31
+ }
32
+ export function defineAutoUnique(uniqueParameters) {
33
+ const { documentId, name } = uniqueParameters;
34
+ let tag;
35
+ let rawElement;
36
+ return {
37
+ [UNIQUE_PREFIX]: true,
38
+ auto: true,
39
+ documentId,
40
+ name,
41
+ get tag() {
42
+ if (!tag) {
43
+ throw new TSProseError(`Auto unique "${name}" tag is not assigned yet!`);
44
+ }
45
+ return tag;
46
+ },
47
+ set tag(value) {
48
+ if (tag) {
49
+ throw new TSProseError(`Auto unique "${name}" tag is already assigned and cannot be reassigned!`);
50
+ }
51
+ tag = value;
52
+ },
53
+ get rawElement() {
54
+ if (!rawElement) {
55
+ throw new TSProseError(`Auto unique "${name}" raw element is not assigned yet!`);
56
+ }
57
+ return rawElement;
58
+ },
59
+ set rawElement(value) {
60
+ if (rawElement) {
61
+ throw new TSProseError(`Auto unique "${name}" raw element is already assigned and cannot be reassigned!`);
62
+ }
63
+ if (!value.schema.linkable) {
64
+ throw new TSProseError(`Auto unique "${name}" cannot be assigned with non-linkable <${value.tagName}>!`);
65
+ }
66
+ rawElement = value;
67
+ },
68
+ };
69
+ }
70
+ export function isUnique(unique, tag) {
71
+ if (unique?.[UNIQUE_PREFIX] === true) {
72
+ if (tag) {
73
+ return unique.tag === tag;
74
+ }
75
+ return true;
76
+ }
77
+ return false;
78
+ }
79
+ export function isAutoUnique(unique) {
80
+ return isUnique(unique) && unique.auto === true;
81
+ }
@@ -0,0 +1 @@
1
+ export declare function hash(text: string, length: number): string;
@@ -0,0 +1,13 @@
1
+ export function hash(text, length) {
2
+ const ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
3
+ let h = 5381;
4
+ for (let i = 0; i < text.length; i++) {
5
+ h = ((h << 5) + h + text.charCodeAt(i)) >>> 0;
6
+ }
7
+ let out = '';
8
+ for (let i = 0; i < length; i++) {
9
+ out += ALPHABET[h % 62];
10
+ h = ((h << 5) + h + i) >>> 0;
11
+ }
12
+ return out;
13
+ }
package/dist/walk.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import type { ProseElement, RawElement } from './element.js';
2
+ export declare const WalkStop: {
3
+ __TSPROSE_walkStop: boolean;
4
+ };
5
+ export declare const WalkNoDeeper: {
6
+ __TSPROSE_walkNoDeeper: boolean;
7
+ };
8
+ export declare function isWalkStop(value: any): value is typeof WalkStop;
9
+ export declare function isWalkNoDeeper(value: any): value is typeof WalkNoDeeper;
10
+ export declare function walkPreSync<ElementKind extends RawElement | ProseElement>(element: ElementKind, walkStep: (element: ElementKind) => void | typeof WalkStop | typeof WalkNoDeeper): typeof WalkStop | void;
11
+ export declare function walkPre<ElementKind extends RawElement | ProseElement>(element: ElementKind, walkStep: (element: ElementKind) => Promise<void | typeof WalkStop | typeof WalkNoDeeper>): Promise<typeof WalkStop | void>;
12
+ export declare function walkPostSync<ElementKind extends RawElement | ProseElement, StepReturn = any>(element: ElementKind, walkStep: (element: ElementKind, childrenResults: StepReturn[]) => StepReturn | typeof WalkStop): StepReturn | typeof WalkStop;
13
+ export declare function walkPost<ElementKind extends RawElement | ProseElement, StepReturn = any>(element: ElementKind, walkStep: (element: ElementKind, childrenResults: StepReturn[]) => Promise<StepReturn | typeof WalkStop>): Promise<StepReturn | typeof WalkStop>;