Skip to content

Commit

Permalink
feat: Improved typings and new features.
Browse files Browse the repository at this point in the history
Improved, independent typings for extensions. New method for setting last index. Change! - Preserve
last index through multiple executions of the same method. Improved unicode and range functions.
Allow using other ReXer instances for operations and nesting.

BREAKING CHANGE: Preserve last index throughout multiple executions of same method.
  • Loading branch information
Kera2024 committed Dec 21, 2018
1 parent ac8db33 commit 0607b74
Show file tree
Hide file tree
Showing 21 changed files with 916 additions and 809 deletions.
16 changes: 9 additions & 7 deletions src/extensions/anchors.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
/**
* @module AnchorsExtension
*/
import { Method } from '../rexer'
import { Matcher } from '../matcher'
import { Method, ReXer } from '../rexer'

export interface AnchorsExtension {
/**
* Matches beginning of passed string or line ( if multiline is applied ).
*/
begin(): Matcher
begin(): this
/**
* Matches ending of passed string or line ( if multiline is applied ).
*/
end(): Matcher
end(): this
/**
* States that match starts with non-word character e.g. space, -, tab etc.
*/
wordBoundary(): Matcher
boundary: AnchorsExtension['wordBoundary']
wordBoundary(): this
/**
* States that match starts with non-word character e.g. space, -, tab etc.
*/
boundary(): this
}
/**
* RegEx anchor-related methods ReX.js extension.
*/
export const AnchorsExtension: Method<Matcher>[] = [
export const AnchorsExtension: Method<ReXer>[] = [
{
name: 'begin',
func() {
Expand Down
50 changes: 34 additions & 16 deletions src/extensions/characters.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,59 @@
/**
* @module CharactersExtension
*/
import { Method } from '../rexer'
import { Matcher } from '../matcher'
import { Method, ReXer } from '../rexer'
import { FuncExpr } from '../operation'

export interface CharactersExtension {
/**
* Matches any character except line breaks.
* Matches any character except line breaks and special unicodes.
*/
anyButBreak(): Matcher
any: CharactersExtension['anyButBreak']
anyButBreak(): this
/**
* Matches any character except line breaks and special unicodes.
*/
any(): this
/**
* Matches any whitespace character - spaces, tabs, line breaks.
*/
anyWhitespace(): this
/**
* Matches any whitespace character - spaces, tabs, line breaks.
*/
anyWhitespace(): Matcher
whitespace: CharactersExtension['anyWhitespace']
whitespace(): this
/**
* Matches any digit.
*/
digit(): Matcher
digit(): this
/**
* Matches any word character - upper & lower case letters, numbers and underscores.
*/
wordChar(): Matcher
char: CharactersExtension['wordChar']
wordChar(): this
/**
* Matches any word character - upper & lower case letters, numbers and underscores.
*/
char(): this
/**
* Matches set of chararacters of same type between desired limits.
* @param start - Range starting character.
* @param end - Range ending character of same type e.g. 1,5 or a,f
* @param more - Pass more ranges in form of two arguments pairs.
*/
range<T extends string | number>(start: T, end: T): Matcher
range<T extends string | number>(start: T, end: T, ...more: (string | number)[]): this
/**
* Matches set of characters.
* @param set - String of characters to be included in set.
*/
set(set: string | ((rex: Matcher) => void)): Matcher
set(set: string | FuncExpr<this>): this
}

/**
* RegEx character matching methods ReX.js extension.
*/
export const CharactersExtension: Method<Matcher>[] = [
export const CharactersExtension: Method<ReXer>[] = [
{
name: 'set',
func(set: string | FuncExpr<Matcher>) {
func(set: string | FuncExpr<ReXer>) {
let expr
if (typeof set === 'string') {
expr = set.replace(/\\|]|-/g, '\\$&')
Expand All @@ -70,13 +79,22 @@ export const CharactersExtension: Method<Matcher>[] = [
},
{
name: 'range',
func(start: string | number, end: string | number) {
func(start: string | number, end: string | number, ...more: (string | number)[]) {
let expr = `${start}-${end}`
const length = more ? more.length : 0
if (length > 0 && length % 2 === 0) {
for (let i = 0; i < length; i += 2) {
expr += `${more[i]}-${more[i + 1]}`
}
} else if (length % 2 !== 0) {
console.warn('Invalid range - omitted!')
}
this.add({
expr,
closure: {
close: ']',
open: '[',
},
expr: `${start}-${end}`,
negate() {
this.setClosure({
close: ']',
Expand Down
41 changes: 23 additions & 18 deletions src/extensions/escaped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,61 @@
* @module EscapedCharactersExtension
*/
import { Method } from '../rexer'
import { Matcher } from '../matcher'
import { Matcher } from 'src/matcher'

export interface EscapedCharactersExtension {
/**
* Matches carriage return character ( char code 13 ).
*/
carriageReturn(): Matcher
carriageReturn(): this
/**
* Matches null character ( char code 0 ).
*/
codeZero(): Matcher
codeZero(): this
/**
* Matches given control escaped character.
* @param letter - Control letter ( A for char code 1 up to Z for char code 26 )
*/
control(letter: string): Matcher
control(letter: string): this
/**
* Matches line feed character a.k.a. 'enter' ( char code 10 ).
*/
lineFeed(): Matcher
enter: EscapedCharactersExtension['lineFeed']
lineFeed(): this
/**
* Matches line feed character a.k.a. 'enter' ( char code 10 ).
*/
enter(): this
/**
* Matches form feed character ( char code 12 ).
*/
formFeed(): Matcher
formFeed(): this
/**
* Matches given hexadecimal escaped character.
* @param hex - Sting of two hex digits e.g. "ff".
*/
hex(hex: string): Matcher
hex(hex: string): this
/**
* Matches given octal escaped character.
* @param oct - Sting of three octal digits e.g. "021" with max being "377".
*/
octal(oct: string): Matcher
octal(oct: string): this
/**
* Matches tab character ( char code 9 ).
*/
tab(): Matcher
tab(): this
/**
* Matches given unicode escaped character.
* @param code - Sting of four ( can be more with extendedUnicodes applied ) hex digits.
*/
unicode(code: string): Matcher
unicode(code: string): this
/**
* Matches vertical tab character ( char code 11 ).
*/
verticalTab(): this
/**
* Matches vertical tab character ( char code 11 ).
*/
verticalTab(): Matcher
vTab: EscapedCharactersExtension['verticalTab']
vTab(): this
}

/**
Expand Down Expand Up @@ -88,14 +94,13 @@ export const EscapedCharactersExtension: Method<Matcher>[] = [
name: 'unicode',
func(code: string) {
const maxHexDigits: number = 4
if (code.length === maxHexDigits) {
const containsUFlag = this.containsFlag('u')
if (code.length <= maxHexDigits && !containsUFlag) {
this.add(`\\u${code}`)
} else if (code.length > maxHexDigits && this.containsFlag('u')) {
} else if (containsUFlag) {
this.add(`\\u{${code}}`)
} else {
console.warn(
'Not enough characters supplied or too many without `u` flag applied - omitted!',
)
console.warn('Too many characters supplied without `u` flag applied - omitted!')
}

return this
Expand Down
54 changes: 42 additions & 12 deletions src/extensions/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,62 @@ export interface FlagsExtension {
* Allows extended use of unicodes.
* See browser support!
*/
extendedUnicodes(): Matcher
u: FlagsExtension['extendedUnicodes']
extendedUnicodes(): this
/**
* Allows extended use of unicodes.
* See browser support!
*/
u(): this

/**
* Allows to regiser more than 1 matches.
*/
globalize(): this
/**
* Allows to regiser more than 1 matches.
*/
globalize(): Matcher
g: FlagsExtension['globalize']
global: FlagsExtension['globalize']
global(): this
/**
* Allows to regiser more than 1 matches.
*/
g(): this

/**
* Disables case sensitivity.
*/
ignoreCase(): this
/**
* Disables case sensitivity.
*/
ignoreCase(): Matcher
i: FlagsExtension['ignoreCase']
i(): this

/**
* Takes into account new lines.
*/
multiline(): this
/**
* Takes into account new lines.
*/
multiline(): Matcher
m: FlagsExtension['multiline']
m(): this

/**
* Register only one match, starting from selected index.
* Automatically ignores 'globalize()' function
* See browser support!
*/
singleByIndex(): this
/**
* Register only one match, starting from selected index.
* Automatically ignores 'globalize()' function
* See browser support!
*/
sticky(): this
/**
* Register only one match, starting from selected index.
* Automatically ignores 'globalize()' function
* See browser support!
*/
singleByIndex(): Matcher
y: FlagsExtension['singleByIndex']
sticky: FlagsExtension['singleByIndex']
y(): this
}

/**
Expand Down
26 changes: 17 additions & 9 deletions src/extensions/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,45 @@
* @module GroupExtension
*/
import { FuncExpr } from '../operation'
import { Method } from '../rexer'
import { Matcher } from '../matcher'
import { Method, ReXer } from '../rexer'

export interface GroupExtension {
/**
* Sets name of preceding expression.
* @param name - Name for the previously applied expression.
*/
as(name: string): Matcher
as(name: string): this
/**
* Captures multiple characters together.
* Negate creates group of characters but doesn't capture them.
* @param expr - Set's body as string of characters or expression body ( function ).
*/
capture(expr: string | FuncExpr<Matcher>): Matcher
group: GroupExtension['capture']
capture(expr: string | FuncExpr<this> | this): this
/**
* Captures multiple characters together.
* Negate creates group of characters but doesn't capture them.
* @param expr - Set's body as string of characters or expression body ( function ).
*/
group(expr: string | FuncExpr<this> | this): this

/**
* Matches result of previous group.
* @param name - Name of expression to be referenced. Previously set by 'as()' method.
*/
ref(name: string): Matcher
reference: GroupExtension['ref']
ref(name: string): this
/**
* Matches result of previous group.
* @param name - Name of expression to be referenced. Previously set by 'as()' method.
*/
reference(name: string): this
}
/**
* RegEx grouping-related methods ReX.js extension.
*/
export const GroupExtension: Method<Matcher>[] = [
export const GroupExtension: Method<ReXer>[] = [
{
name: ['capture', 'group'],
func(expr: string | FuncExpr<Matcher>) {
func(expr: string | FuncExpr<ReXer> | ReXer) {
this.add({
expr,
closure: {
Expand Down
Loading

0 comments on commit 0607b74

Please sign in to comment.