@bangle.dev/core
Advanced tools
Comparing version 0.19.0 to 0.20.0
@@ -55,3 +55,3 @@ import { Command } from 'prosemirror-commands'; | ||
schema: Schema; | ||
}) => (import("prosemirror-inputrules").InputRule<Schema<any, any>> | import("prosemirror-state").Plugin<any, any>)[]; | ||
}) => (import("prosemirror-state").Plugin<any, any> | import("prosemirror-inputrules").InputRule<Schema<any, any>>)[]; | ||
export declare function queryIsBlockquoteActive(): (state: EditorState) => boolean; | ||
@@ -58,0 +58,0 @@ export declare function wrapInBlockquote(): Command<any>; |
@@ -53,3 +53,3 @@ import { Command } from 'prosemirror-commands'; | ||
schema: Schema; | ||
}) => (import("prosemirror-state").Plugin<any, any> | import("prosemirror-inputrules").InputRule<any>)[]; | ||
}) => (import("prosemirror-inputrules").InputRule<any> | import("prosemirror-state").Plugin<any, any>)[]; | ||
export declare function toggleBold(): Command; | ||
@@ -56,0 +56,0 @@ export declare function queryIsBoldActive(): (state: EditorState) => boolean; |
@@ -53,3 +53,3 @@ import { Command } from 'prosemirror-commands'; | ||
schema: Schema; | ||
}) => (import("prosemirror-inputrules").InputRule<Schema<any, any>> | import("prosemirror-state").Plugin<any, any>)[]; | ||
}) => (import("prosemirror-inputrules").InputRule<any> | import("prosemirror-state").Plugin<any, any>)[]; | ||
export declare function toggleBulletList(): Command; | ||
@@ -56,0 +56,0 @@ export declare function toggleTodoList(): Command; |
@@ -67,5 +67,5 @@ import { Node } from 'prosemirror-model'; | ||
schema: Schema; | ||
}) => (import("prosemirror-inputrules").InputRule<Schema<any, any>> | import("prosemirror-state").Plugin<any, any>)[]; | ||
}) => (import("prosemirror-state").Plugin<any, any> | import("prosemirror-inputrules").InputRule<Schema<any, any>>)[]; | ||
export declare function queryIsCodeActiveBlock(): (state: EditorState) => boolean; | ||
export {}; | ||
//# sourceMappingURL=code-block.d.ts.map |
@@ -45,3 +45,3 @@ import { Command } from 'prosemirror-commands'; | ||
schema: Schema; | ||
}) => (import("prosemirror-state").Plugin<any, any> | import("prosemirror-inputrules").InputRule<any>)[]; | ||
}) => (import("prosemirror-inputrules").InputRule<any> | import("prosemirror-state").Plugin<any, any>)[]; | ||
export declare function toggleCode(): Command; | ||
@@ -48,0 +48,0 @@ export declare function queryIsCodeActive(): (state: EditorState) => boolean; |
@@ -46,3 +46,3 @@ import { Command } from 'prosemirror-commands'; | ||
schema: Schema; | ||
}) => (import("prosemirror-state").Plugin<any, any> | import("prosemirror-inputrules").InputRule<any>)[]; | ||
}) => (import("prosemirror-inputrules").InputRule<any> | import("prosemirror-state").Plugin<any, any>)[]; | ||
export declare function toggleItalic(): Command; | ||
@@ -49,0 +49,0 @@ export declare function queryIsItalicActive(): (state: EditorState) => boolean; |
@@ -40,3 +40,3 @@ import { EditorState } from 'prosemirror-state'; | ||
toMarkdown: { | ||
open(_state: MarkdownSerializerState, mark: Mark, parent: Node, index: number): "<" | "["; | ||
open(_state: MarkdownSerializerState, mark: Mark, parent: Node, index: number): "[" | "<"; | ||
close(state: MarkdownSerializerState, mark: Mark, parent: Node, index: number): string; | ||
@@ -43,0 +43,0 @@ }; |
@@ -58,3 +58,3 @@ import { Schema, Node } from 'prosemirror-model'; | ||
schema: Schema; | ||
}) => (import("prosemirror-inputrules").InputRule<Schema<any, any>> | import("prosemirror-state").Plugin<any, any>)[]; | ||
}) => (import("prosemirror-state").Plugin<any, any> | import("prosemirror-inputrules").InputRule<Schema<any, any>>)[]; | ||
export declare function toggleOrderedList(): Command; | ||
@@ -61,0 +61,0 @@ export declare function queryIsOrderedListActive(): (state: EditorState) => boolean; |
@@ -48,3 +48,3 @@ import { Command } from 'prosemirror-commands'; | ||
schema: Schema; | ||
}) => (import("prosemirror-state").Plugin<any, any> | import("prosemirror-inputrules").InputRule<any>)[]; | ||
}) => (import("prosemirror-inputrules").InputRule<any> | import("prosemirror-state").Plugin<any, any>)[]; | ||
export declare function toggleStrike(): Command; | ||
@@ -51,0 +51,0 @@ export declare function queryIsStrikeActive(): (state: EditorState) => boolean; |
@@ -43,3 +43,3 @@ import { Command } from 'prosemirror-commands'; | ||
schema: Schema; | ||
}) => (import("prosemirror-state").Plugin<any, any> | import("prosemirror-inputrules").InputRule<any>)[]; | ||
}) => (import("prosemirror-inputrules").InputRule<any> | import("prosemirror-state").Plugin<any, any>)[]; | ||
export declare function toggleUnderline(): Command; | ||
@@ -46,0 +46,0 @@ export declare function queryIsUnderlineActive(): (state: EditorState) => boolean; |
@@ -5,24 +5,45 @@ export function defaultSpecs(opts?: {}): (false | { | ||
schema: { | ||
group: string; | ||
}; | ||
markdown: { | ||
toMarkdown(state: any, node: import("prosemirror-model").Node<any>): void; | ||
}; | ||
} | { | ||
type: string; | ||
topNode: boolean; | ||
name: string; | ||
schema: { | ||
content: string; | ||
defining: boolean; | ||
draggable: boolean; | ||
}; | ||
} | { | ||
type: string; | ||
name: string; | ||
schema: { | ||
attrs: { | ||
todoChecked: { | ||
href: { | ||
default: null; | ||
}; | ||
}; | ||
toDOM: (node: import("prosemirror-model").Node<any>) => any[]; | ||
inclusive: boolean; | ||
parseDOM: { | ||
priority: number; | ||
tag: string; | ||
getAttrs: (dom: Element) => any; | ||
getAttrs: (dom: HTMLElement) => { | ||
href: string | null; | ||
}; | ||
}[]; | ||
toDOM: (node: import("prosemirror-model").Node<any>) => (string | number | { | ||
rel: string; | ||
})[]; | ||
}; | ||
markdown: { | ||
toMarkdown(state: import("prosemirror-markdown").MarkdownSerializerState<any>, node: import("prosemirror-model").Node<any>): void; | ||
toMarkdown: { | ||
open(_state: import("prosemirror-markdown").MarkdownSerializerState<any>, mark: import("prosemirror-model").Mark<any>, parent: import("prosemirror-model").Node<any>, index: number): "[" | "<"; | ||
close(state: import("prosemirror-markdown").MarkdownSerializerState<any>, mark: import("prosemirror-model").Mark<any>, parent: import("prosemirror-model").Node<any>, index: number): string; | ||
}; | ||
parseMarkdown: { | ||
list_item: { | ||
block: string; | ||
link: { | ||
mark: string; | ||
getAttrs: (tok: import("markdown-it/lib/token")) => { | ||
todoChecked: string | null; | ||
href: string | null; | ||
title: string | null; | ||
}; | ||
@@ -32,22 +53,9 @@ }; | ||
}; | ||
} | { | ||
type: string; | ||
name: string; | ||
schema: { | ||
group: string; | ||
options: { | ||
openOnClick: boolean; | ||
}; | ||
markdown: { | ||
toMarkdown(state: any, node: import("prosemirror-model").Node<any>): void; | ||
}; | ||
} | { | ||
type: string; | ||
topNode: boolean; | ||
name: string; | ||
schema: { | ||
content: string; | ||
}; | ||
} | { | ||
type: string; | ||
name: string; | ||
schema: { | ||
parseDOM: ({ | ||
@@ -134,40 +142,2 @@ tag: string; | ||
schema: { | ||
attrs: { | ||
href: { | ||
default: null; | ||
}; | ||
}; | ||
inclusive: boolean; | ||
parseDOM: { | ||
tag: string; | ||
getAttrs: (dom: HTMLElement) => { | ||
href: string | null; | ||
}; | ||
}[]; | ||
toDOM: (node: import("prosemirror-model").Node<any>) => (string | number | { | ||
rel: string; | ||
})[]; | ||
}; | ||
markdown: { | ||
toMarkdown: { | ||
open(_state: import("prosemirror-markdown").MarkdownSerializerState<any>, mark: import("prosemirror-model").Mark<any>, parent: import("prosemirror-model").Node<any>, index: number): "<" | "["; | ||
close(state: import("prosemirror-markdown").MarkdownSerializerState<any>, mark: import("prosemirror-model").Mark<any>, parent: import("prosemirror-model").Node<any>, index: number): string; | ||
}; | ||
parseMarkdown: { | ||
link: { | ||
mark: string; | ||
getAttrs: (tok: import("markdown-it/lib/token")) => { | ||
href: string | null; | ||
title: string | null; | ||
}; | ||
}; | ||
}; | ||
}; | ||
options: { | ||
openOnClick: boolean; | ||
}; | ||
} | { | ||
type: string; | ||
name: string; | ||
schema: { | ||
parseDOM: ({ | ||
@@ -212,7 +182,37 @@ tag: string; | ||
}; | ||
} | { | ||
type: string; | ||
name: string; | ||
schema: { | ||
content: string; | ||
defining: boolean; | ||
draggable: boolean; | ||
attrs: { | ||
todoChecked: { | ||
default: null; | ||
}; | ||
}; | ||
toDOM: (node: import("prosemirror-model").Node<any>) => any[]; | ||
parseDOM: { | ||
priority: number; | ||
tag: string; | ||
getAttrs: (dom: Element) => any; | ||
}[]; | ||
}; | ||
markdown: { | ||
toMarkdown(state: import("prosemirror-markdown").MarkdownSerializerState<any>, node: import("prosemirror-model").Node<any>): void; | ||
parseMarkdown: { | ||
list_item: { | ||
block: string; | ||
getAttrs: (tok: import("markdown-it/lib/token")) => { | ||
todoChecked: string | null; | ||
}; | ||
}; | ||
}; | ||
}; | ||
})[]; | ||
export function defaultPlugins(opts?: {}): (false | (({ schema, specRegistry }: { | ||
export function defaultPlugins(opts?: {}): (false | (() => import("..").PluginGroup) | (({ schema, specRegistry }: { | ||
schema: import("prosemirror-model").Schema<any, any>; | ||
specRegistry: any; | ||
}) => any[]) | (() => import("..").PluginGroup))[]; | ||
}) => any[]))[]; | ||
//# sourceMappingURL=default-components.d.ts.map |
@@ -6,24 +6,45 @@ /// <reference types="prosemirror-model" /> | ||
schema: { | ||
group: string; | ||
}; | ||
markdown: { | ||
toMarkdown(state: any, node: import("prosemirror-model").Node<any>): void; | ||
}; | ||
} | { | ||
type: string; | ||
topNode: boolean; | ||
name: string; | ||
schema: { | ||
content: string; | ||
defining: boolean; | ||
draggable: boolean; | ||
}; | ||
} | { | ||
type: string; | ||
name: string; | ||
schema: { | ||
attrs: { | ||
todoChecked: { | ||
href: { | ||
default: null; | ||
}; | ||
}; | ||
toDOM: (node: import("prosemirror-model").Node<any>) => any[]; | ||
inclusive: boolean; | ||
parseDOM: { | ||
priority: number; | ||
tag: string; | ||
getAttrs: (dom: Element) => any; | ||
getAttrs: (dom: HTMLElement) => { | ||
href: string | null; | ||
}; | ||
}[]; | ||
toDOM: (node: import("prosemirror-model").Node<any>) => (string | number | { | ||
rel: string; | ||
})[]; | ||
}; | ||
markdown: { | ||
toMarkdown(state: import("prosemirror-markdown").MarkdownSerializerState<any>, node: import("prosemirror-model").Node<any>): void; | ||
toMarkdown: { | ||
open(_state: import("prosemirror-markdown").MarkdownSerializerState<any>, mark: import("prosemirror-model").Mark<any>, parent: import("prosemirror-model").Node<any>, index: number): "[" | "<"; | ||
close(state: import("prosemirror-markdown").MarkdownSerializerState<any>, mark: import("prosemirror-model").Mark<any>, parent: import("prosemirror-model").Node<any>, index: number): string; | ||
}; | ||
parseMarkdown: { | ||
list_item: { | ||
block: string; | ||
link: { | ||
mark: string; | ||
getAttrs: (tok: import("markdown-it/lib/token")) => { | ||
todoChecked: string | null; | ||
href: string | null; | ||
title: string | null; | ||
}; | ||
@@ -33,22 +54,9 @@ }; | ||
}; | ||
} | { | ||
type: string; | ||
name: string; | ||
schema: { | ||
group: string; | ||
options: { | ||
openOnClick: boolean; | ||
}; | ||
markdown: { | ||
toMarkdown(state: any, node: import("prosemirror-model").Node<any>): void; | ||
}; | ||
} | { | ||
type: string; | ||
topNode: boolean; | ||
name: string; | ||
schema: { | ||
content: string; | ||
}; | ||
} | { | ||
type: string; | ||
name: string; | ||
schema: { | ||
parseDOM: ({ | ||
@@ -135,40 +143,2 @@ tag: string; | ||
schema: { | ||
attrs: { | ||
href: { | ||
default: null; | ||
}; | ||
}; | ||
inclusive: boolean; | ||
parseDOM: { | ||
tag: string; | ||
getAttrs: (dom: HTMLElement) => { | ||
href: string | null; | ||
}; | ||
}[]; | ||
toDOM: (node: import("prosemirror-model").Node<any>) => (string | number | { | ||
rel: string; | ||
})[]; | ||
}; | ||
markdown: { | ||
toMarkdown: { | ||
open(_state: import("prosemirror-markdown").MarkdownSerializerState<any>, mark: import("prosemirror-model").Mark<any>, parent: import("prosemirror-model").Node<any>, index: number): "<" | "["; | ||
close(state: import("prosemirror-markdown").MarkdownSerializerState<any>, mark: import("prosemirror-model").Mark<any>, parent: import("prosemirror-model").Node<any>, index: number): string; | ||
}; | ||
parseMarkdown: { | ||
link: { | ||
mark: string; | ||
getAttrs: (tok: import("markdown-it/lib/token")) => { | ||
href: string | null; | ||
title: string | null; | ||
}; | ||
}; | ||
}; | ||
}; | ||
options: { | ||
openOnClick: boolean; | ||
}; | ||
} | { | ||
type: string; | ||
name: string; | ||
schema: { | ||
parseDOM: ({ | ||
@@ -213,7 +183,37 @@ tag: string; | ||
}; | ||
} | { | ||
type: string; | ||
name: string; | ||
schema: { | ||
content: string; | ||
defining: boolean; | ||
draggable: boolean; | ||
attrs: { | ||
todoChecked: { | ||
default: null; | ||
}; | ||
}; | ||
toDOM: (node: import("prosemirror-model").Node<any>) => any[]; | ||
parseDOM: { | ||
priority: number; | ||
tag: string; | ||
getAttrs: (dom: Element) => any; | ||
}[]; | ||
}; | ||
markdown: { | ||
toMarkdown(state: import("prosemirror-markdown").MarkdownSerializerState<any>, node: import("prosemirror-model").Node<any>): void; | ||
parseMarkdown: { | ||
list_item: { | ||
block: string; | ||
getAttrs: (tok: import("markdown-it/lib/token")) => { | ||
todoChecked: string | null; | ||
}; | ||
}; | ||
}; | ||
}; | ||
})[]; | ||
export declare function corePlugins(): ((({ schema, specRegistry }: { | ||
export declare function corePlugins(): ((() => import("..").PluginGroup) | (({ schema, specRegistry }: { | ||
schema: import("prosemirror-model").Schema<any, any>; | ||
specRegistry: any; | ||
}) => any[]) | (() => import("..").PluginGroup))[]; | ||
}) => any[]))[]; | ||
//# sourceMappingURL=core-components.d.ts.map |
@@ -9,8 +9,11 @@ import { DOMOutputSpec } from 'prosemirror-model'; | ||
export declare function compose(func: Function, ...funcs: Function[]): (raw: any) => any; | ||
export declare function matchAllPlus(regexp: RegExp, str: string): { | ||
declare class MatchType { | ||
start: number; | ||
end: number; | ||
match: boolean; | ||
subString: string; | ||
}[]; | ||
private sourceString; | ||
constructor(start: number, end: number, match: boolean, sourceString: string); | ||
get subString(): string; | ||
} | ||
export declare function matchAllPlus(regexp: RegExp, str: string): MatchType[]; | ||
export declare function uuid(len?: number): string; | ||
@@ -17,0 +20,0 @@ export declare function getIdleCallback(cb: Function): any; |
@@ -44,42 +44,53 @@ import { DOMSerializer } from 'prosemirror-model'; | ||
} | ||
class MatchType { | ||
constructor(start, end, match, sourceString) { | ||
Object.defineProperty(this, "start", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: start | ||
}); | ||
Object.defineProperty(this, "end", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: end | ||
}); | ||
Object.defineProperty(this, "match", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: match | ||
}); | ||
Object.defineProperty(this, "sourceString", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: sourceString | ||
}); | ||
} | ||
get subString() { | ||
return this.sourceString.slice(this.start, this.end); | ||
} | ||
} | ||
export function matchAllPlus(regexp, str) { | ||
const matches = Array.from(str.matchAll(regexp)); | ||
if (matches.length === 0) { | ||
return [ | ||
{ | ||
start: 0, | ||
end: str.length, | ||
match: false, | ||
subString: str, | ||
}, | ||
]; | ||
} | ||
let result = []; | ||
const result = []; | ||
let prevElementEnd = 0; | ||
for (let cur of matches) { | ||
let curStart = cur.index; | ||
let curEnd = curStart + cur[0].length; | ||
let match; | ||
while ((match = regexp.exec(str))) { | ||
const curStart = match.index; | ||
const curEnd = curStart + match[0].length; | ||
if (prevElementEnd !== curStart) { | ||
result.push({ | ||
start: prevElementEnd, | ||
end: curStart, | ||
match: false, | ||
}); | ||
result.push(new MatchType(prevElementEnd, curStart, false, str)); | ||
} | ||
result.push({ | ||
start: curStart, | ||
end: curEnd, | ||
match: true, | ||
}); | ||
result.push(new MatchType(curStart, curEnd, true, str)); | ||
prevElementEnd = curEnd; | ||
} | ||
if (result.length === 0) { | ||
return [new MatchType(0, str.length, false, str)]; | ||
} | ||
const lastItemEnd = result[result.length - 1] && result[result.length - 1].end; | ||
if (lastItemEnd && lastItemEnd !== str.length) { | ||
result.push({ | ||
start: lastItemEnd, | ||
end: str.length, | ||
match: false, | ||
}); | ||
result.push(new MatchType(lastItemEnd, str.length, false, str)); | ||
} | ||
result = result.map((r) => ({ ...r, subString: str.slice(r.start, r.end) })); | ||
return result; | ||
@@ -86,0 +97,0 @@ } |
{ | ||
"name": "@bangle.dev/core", | ||
"version": "0.19.0", | ||
"version": "0.20.0", | ||
"homepage": "https://bangle.dev", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -17,2 +17,8 @@ /** | ||
describe('matchAllPlus', () => { | ||
const mergeStartEnd = (str, result) => | ||
result.reduce((prev, cur) => prev + str.slice(cur.start, cur.end), ''); | ||
const mergeSubstrings = (str, result) => | ||
result.reduce((prev, cur) => prev + cur.subString, ''); | ||
test('works when match', () => { | ||
@@ -26,3 +32,3 @@ const result = matchAllPlus(/foo[a-z]*/g, 'baseball foozball'); | ||
`); | ||
expect(result).toMatchSnapshot(); | ||
expect(result.map((r) => ({ ...r }))).toMatchSnapshot(); | ||
}); | ||
@@ -37,3 +43,3 @@ | ||
`); | ||
expect(result).toMatchSnapshot(); | ||
expect(result.map((r) => ({ ...r }))).toMatchSnapshot(); | ||
}); | ||
@@ -49,3 +55,3 @@ | ||
expect(result.every((r) => r.match === false)).toBe(true); | ||
expect(result).toMatchSnapshot(); | ||
expect(result.map((r) => ({ ...r }))).toMatchSnapshot(); | ||
}); | ||
@@ -103,6 +109,6 @@ | ||
'%# string start and end positions should be correct', | ||
(str, matchCount) => { | ||
(string, matchCount) => { | ||
const result = matchAllPlus( | ||
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-zA-Z]{2,}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g, | ||
str, | ||
string, | ||
); | ||
@@ -112,7 +118,73 @@ | ||
expect( | ||
result.reduce((prev, cur) => prev + str.slice(cur.start, cur.end), ''), | ||
).toBe(str); | ||
expect(mergeStartEnd(string, result)).toBe(string); | ||
}, | ||
); | ||
test('1 misc cases', () => { | ||
const regex = /t(e)(st(\d?))/g; | ||
const string = 'test1test2'; | ||
const result = matchAllPlus(regex, string); | ||
expect(mergeStartEnd(string, result)).toBe(string); | ||
expect(mergeSubstrings(string, result)).toBe(string); | ||
expect(result).toMatchInlineSnapshot(` | ||
Array [ | ||
MatchType { | ||
"end": 5, | ||
"match": true, | ||
"sourceString": "test1test2", | ||
"start": 0, | ||
}, | ||
MatchType { | ||
"end": 10, | ||
"match": true, | ||
"sourceString": "test1test2", | ||
"start": 5, | ||
}, | ||
] | ||
`); | ||
}); | ||
test('2 misc cases', () => { | ||
const regex = /(#\w+)/g; | ||
const string = 'Hello #world #planet!'; | ||
const result = matchAllPlus(regex, string); | ||
expect(mergeStartEnd(string, result)).toBe(string); | ||
expect(result).toMatchInlineSnapshot(` | ||
Array [ | ||
MatchType { | ||
"end": 6, | ||
"match": false, | ||
"sourceString": "Hello #world #planet!", | ||
"start": 0, | ||
}, | ||
MatchType { | ||
"end": 12, | ||
"match": true, | ||
"sourceString": "Hello #world #planet!", | ||
"start": 6, | ||
}, | ||
MatchType { | ||
"end": 13, | ||
"match": false, | ||
"sourceString": "Hello #world #planet!", | ||
"start": 12, | ||
}, | ||
MatchType { | ||
"end": 20, | ||
"match": true, | ||
"sourceString": "Hello #world #planet!", | ||
"start": 13, | ||
}, | ||
MatchType { | ||
"end": 21, | ||
"match": false, | ||
"sourceString": "Hello #world #planet!", | ||
"start": 20, | ||
}, | ||
] | ||
`); | ||
}); | ||
}); | ||
@@ -119,0 +191,0 @@ |
@@ -58,58 +58,48 @@ import { DOMOutputSpec, DOMSerializer } from 'prosemirror-model'; | ||
class MatchType { | ||
constructor( | ||
public start: number, | ||
public end: number, | ||
public match: boolean, | ||
private sourceString: string, | ||
) {} | ||
get subString() { | ||
return this.sourceString.slice(this.start, this.end); | ||
} | ||
} | ||
/** | ||
* Runs matchAll and gives range of strings that matched and didnt match | ||
* | ||
* @param {*} regexp | ||
* @param {*} str | ||
* Returns an array of objects which contains a range of substring and whether it matched or didn't match. | ||
* Note: each item in this array will map 1:1 in order with the original string in a way | ||
* such that following will always hold true: | ||
* ``` | ||
* const result = matchAllPlus(regex, myStr); | ||
* result.reduce((a, b) => a + b.subString) === myStr | ||
* result.reduce((a, b) => a + b.slice(b.start, b.end)) === myStr | ||
* ``` | ||
*/ | ||
export function matchAllPlus(regexp: RegExp, str: string) { | ||
// @ts-ignore String.prototype.matchAll is pretty new. Even adding | ||
// @types/string.prototype.matchall doesn't help. | ||
const matches: RegExpMatchArray[] = Array.from(str.matchAll(regexp)); | ||
if (matches.length === 0) { | ||
return [ | ||
{ | ||
start: 0, | ||
end: str.length, | ||
match: false, | ||
subString: str, | ||
}, | ||
]; | ||
} | ||
let result = []; | ||
export function matchAllPlus(regexp: RegExp, str: string): MatchType[] { | ||
const result: MatchType[] = []; | ||
let prevElementEnd = 0; | ||
for (let cur of matches) { | ||
let curStart = cur.index!; | ||
// TODO there was an error saying length of undefined in this function | ||
// I suspect it is coming from line below. But not sure how to reproduce it. | ||
let curEnd = curStart + cur[0]!.length; | ||
let match: RegExpExecArray | null; | ||
while ((match = regexp.exec(str))) { | ||
const curStart = match.index; | ||
const curEnd = curStart + match[0].length; | ||
if (prevElementEnd !== curStart) { | ||
result.push({ | ||
start: prevElementEnd, | ||
end: curStart, | ||
match: false, | ||
}); | ||
result.push(new MatchType(prevElementEnd, curStart, false, str)); | ||
} | ||
result.push({ | ||
start: curStart, | ||
end: curEnd, | ||
match: true, | ||
}); | ||
result.push(new MatchType(curStart, curEnd, true, str)); | ||
prevElementEnd = curEnd; | ||
} | ||
if (result.length === 0) { | ||
return [new MatchType(0, str.length, false, str)]; | ||
} | ||
const lastItemEnd = | ||
result[result.length - 1]! && result[result.length - 1]!.end; | ||
result[result.length - 1] && result[result.length - 1].end; | ||
if (lastItemEnd && lastItemEnd !== str.length) { | ||
result.push({ | ||
start: lastItemEnd, | ||
end: str.length, | ||
match: false, | ||
}); | ||
result.push(new MatchType(lastItemEnd, str.length, false, str)); | ||
} | ||
result = result.map((r) => ({ ...r, subString: str.slice(r.start, r.end) })); | ||
return result; | ||
@@ -116,0 +106,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1020253
27424