@oozcitak/util
Advanced tools
Comparing version 1.0.6 to 2.0.0
export { ObjectCache } from './ObjectCache'; | ||
export { CompareCache } from './CompareCache'; | ||
export { StringWalker } from './StringWalker'; | ||
export { StringWalker, SeekOrigin } from './StringWalker'; | ||
export { Lazy } from './Lazy'; | ||
@@ -5,0 +5,0 @@ /** |
@@ -9,2 +9,3 @@ "use strict"; | ||
exports.StringWalker = StringWalker_1.StringWalker; | ||
exports.SeekOrigin = StringWalker_1.SeekOrigin; | ||
var Lazy_1 = require("./Lazy"); | ||
@@ -11,0 +12,0 @@ exports.Lazy = Lazy_1.Lazy; |
/** | ||
* Walks through the code points of a string. | ||
* Walks the code points of a string. | ||
*/ | ||
export declare class StringWalker { | ||
private _chars; | ||
private _index; | ||
private _length; | ||
private _pointer; | ||
private _codePoint; | ||
private _c; | ||
private _remaining; | ||
private _substring; | ||
private _c?; | ||
private _codePoint?; | ||
private _isSurrogatePair; | ||
private _first; | ||
private _second; | ||
/** | ||
@@ -23,32 +24,57 @@ * Initializes a new `StringWalker`. | ||
/** | ||
* Returns the number of code points in the input string. | ||
* Returns the current code point. Returns `-1` if the current position is | ||
* beyond the end of string. | ||
*/ | ||
get length(): number; | ||
get codePoint(): number; | ||
/** | ||
* Returns the current code point. Returns `-1` if the position is beyond | ||
* the end of string. | ||
* Returns the current character. Returns an empty string if the current | ||
* position is beyond the end of string. | ||
*/ | ||
codePoint(): number; | ||
get c(): string; | ||
/** | ||
* Returns the current character. Returns an empty string if the position is | ||
* beyond the end of string. | ||
* Returns the substring including the current character without changing | ||
* the current position. | ||
* | ||
* @param count - the number of code points to return | ||
*/ | ||
c(): string; | ||
peek(count?: number): string; | ||
/** | ||
* Returns the remaining string. | ||
* Determines whether the substring including the current character | ||
* starts with the given string. | ||
* | ||
* @param count - the number of characters to return | ||
* @param match - the string to match | ||
*/ | ||
remaining(count?: number): string; | ||
startsWith(match: string, ignoreCase?: boolean): boolean; | ||
/** | ||
* Returns the substring from the current character to the end of string. | ||
* Moves to the next code point. | ||
*/ | ||
next(): boolean; | ||
/** | ||
* Moves to the previous code point. | ||
*/ | ||
prev(): boolean; | ||
/** | ||
* Seeks a number of code points relative to the current position. | ||
* | ||
* @param count - the number of characters to return | ||
* @param count - number of code points to seek | ||
* @param reference - reference of the seek operation | ||
*/ | ||
substring(count?: number): string; | ||
seek(count: number, reference?: SeekOrigin): void; | ||
} | ||
/** | ||
* Defines the origin of a seek operation. | ||
*/ | ||
export declare enum SeekOrigin { | ||
/** | ||
* Gets or sets the current position. | ||
* Seek relative to the start of the string. | ||
*/ | ||
get pointer(): number; | ||
set pointer(val: number); | ||
Start = -1, | ||
/** | ||
* Seek relative to the current position. | ||
*/ | ||
Current = 0, | ||
/** | ||
* Seek relative to the end of the string. | ||
*/ | ||
End = 1 | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Walks through the code points of a string. | ||
* Walks the code points of a string. | ||
*/ | ||
@@ -13,5 +13,12 @@ class StringWalker { | ||
constructor(input) { | ||
this._pointer = 0; | ||
this._chars = Array.from(input); | ||
this._isSurrogatePair = false; | ||
this._first = -1; | ||
this._second = -1; | ||
this._chars = input; | ||
this._index = 0; | ||
this._length = this._chars.length; | ||
this._first = this._index < this._length ? this._chars.charCodeAt(this._index) : -1; | ||
this._second = this._index < this._length - 1 ? this._chars.charCodeAt(this._index + 1) : -1; | ||
this._isSurrogatePair = (this._first >= 0xD800 && this._first <= 0xDBFF && | ||
this._second >= 0xDC00 && this._second <= 0xDFFF); | ||
} | ||
@@ -21,25 +28,18 @@ /** | ||
*/ | ||
get eof() { return this._pointer >= this._length; } | ||
get eof() { return this._index >= this._length; } | ||
/** | ||
* Returns the number of code points in the input string. | ||
* Returns the current code point. Returns `-1` if the current position is | ||
* beyond the end of string. | ||
*/ | ||
get length() { return this._length; } | ||
/** | ||
* Returns the current code point. Returns `-1` if the position is beyond | ||
* the end of string. | ||
*/ | ||
codePoint() { | ||
get codePoint() { | ||
if (this._codePoint === undefined) { | ||
if (this.eof) { | ||
if (this._first === -1) { | ||
this._codePoint = -1; | ||
} | ||
else if (this._isSurrogatePair) { | ||
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae | ||
this._codePoint = (this._first - 0xD800) * 0x400 + this._second - 0xDC00 + 0x10000; | ||
} | ||
else { | ||
const cp = this._chars[this._pointer].codePointAt(0); | ||
/* istanbul ignore else */ | ||
if (cp !== undefined) { | ||
this._codePoint = cp; | ||
} | ||
else { | ||
this._codePoint = -1; | ||
} | ||
this._codePoint = this._first; | ||
} | ||
@@ -50,8 +50,9 @@ } | ||
/** | ||
* Returns the current character. Returns an empty string if the position is | ||
* beyond the end of string. | ||
* Returns the current character. Returns an empty string if the current | ||
* position is beyond the end of string. | ||
*/ | ||
c() { | ||
get c() { | ||
if (this._c === undefined) { | ||
this._c = (this.eof ? "" : this._chars[this._pointer]); | ||
const cp = this.codePoint; | ||
this._c = (cp === -1 ? "" : String.fromCodePoint(cp)); | ||
} | ||
@@ -61,38 +62,146 @@ return this._c; | ||
/** | ||
* Returns the remaining string. | ||
* Returns the substring including the current character without changing | ||
* the current position. | ||
* | ||
* @param count - the number of characters to return | ||
* @param count - the number of code points to return | ||
*/ | ||
remaining(count) { | ||
if (this._remaining === undefined) { | ||
this._remaining = (this.eof ? [] : this._chars.slice(this._pointer + 1)); | ||
peek(count) { | ||
if (this.eof) | ||
return ""; | ||
if (count === undefined) | ||
return this._chars.slice(this._index); | ||
const originalIndex = this._index; | ||
let charCount = 0; | ||
let n = 0; | ||
while (n < count) { | ||
charCount += this._isSurrogatePair ? 2 : 1; | ||
n++; | ||
if (!this.next()) | ||
break; | ||
} | ||
return (count === undefined ? this._remaining : this._remaining.slice(0, count)).join(''); | ||
this._codePoint = undefined; | ||
this._c = undefined; | ||
this._index = originalIndex; | ||
this._first = this._index < this._length ? this._chars.charCodeAt(this._index) : -1; | ||
this._second = this._index < this._length - 1 ? this._chars.charCodeAt(this._index + 1) : -1; | ||
this._isSurrogatePair = (this._first >= 0xD800 && this._first <= 0xDBFF && | ||
this._second >= 0xDC00 && this._second <= 0xDFFF); | ||
return this._chars.slice(this._index, this._index + charCount); | ||
} | ||
/** | ||
* Returns the substring from the current character to the end of string. | ||
* Determines whether the substring including the current character | ||
* starts with the given string. | ||
* | ||
* @param count - the number of characters to return | ||
* @param match - the string to match | ||
*/ | ||
substring(count) { | ||
if (this._substring === undefined) { | ||
this._substring = (this.eof ? [] : this._chars.slice(this._pointer)); | ||
startsWith(match, ignoreCase = false) { | ||
if (this.eof) | ||
return false; | ||
const len = match.length; | ||
if (len > this._length - this._index) | ||
return false; | ||
if (ignoreCase) { | ||
const chars = this._chars.slice(this._index, this._index + match.length); | ||
if (match.toLowerCase() !== chars.toLowerCase()) | ||
return false; | ||
} | ||
return (count === undefined ? this._substring : this._substring.slice(0, count)).join(''); | ||
else { | ||
for (let i = 0; i < len; i++) { | ||
if (match[i] !== this._chars[this._index + i]) | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* Gets or sets the current position. | ||
* Moves to the next code point. | ||
*/ | ||
get pointer() { return this._pointer; } | ||
set pointer(val) { | ||
if (val === this._pointer) | ||
return; | ||
this._pointer = val; | ||
next() { | ||
if (this.eof) | ||
return false; | ||
this._codePoint = undefined; | ||
this._c = undefined; | ||
this._remaining = undefined; | ||
this._substring = undefined; | ||
this._index += this._isSurrogatePair ? 2 : 1; | ||
this._first = this._index < this._length ? this._chars.charCodeAt(this._index) : -1; | ||
this._second = this._index < this._length - 1 ? this._chars.charCodeAt(this._index + 1) : -1; | ||
this._isSurrogatePair = (this._first >= 0xD800 && this._first <= 0xDBFF && | ||
this._second >= 0xDC00 && this._second <= 0xDFFF); | ||
return true; | ||
} | ||
/** | ||
* Moves to the previous code point. | ||
*/ | ||
prev() { | ||
if (this._index === 0) | ||
return false; | ||
this._codePoint = undefined; | ||
this._c = undefined; | ||
const second = this._index > 0 ? this._chars.charCodeAt(this._index - 1) : -1; | ||
const first = this._index > 1 ? this._chars.charCodeAt(this._index - 2) : -1; | ||
this._isSurrogatePair = (first >= 0xD800 && first <= 0xDBFF && | ||
second >= 0xDC00 && second <= 0xDFFF); | ||
this._index -= this._isSurrogatePair ? 2 : 1; | ||
this._first = this._isSurrogatePair ? first : second; | ||
this._second = this._isSurrogatePair ? second : -1; | ||
return true; | ||
} | ||
/** | ||
* Seeks a number of code points relative to the current position. | ||
* | ||
* @param count - number of code points to seek | ||
* @param reference - reference of the seek operation | ||
*/ | ||
seek(count, reference = SeekOrigin.Current) { | ||
if (reference === SeekOrigin.Start) { | ||
this._codePoint = undefined; | ||
this._c = undefined; | ||
this._index = 0; | ||
this._first = this._index < this._length ? this._chars.charCodeAt(this._index) : -1; | ||
this._second = this._index < this._length - 1 ? this._chars.charCodeAt(this._index + 1) : -1; | ||
this._isSurrogatePair = (this._first >= 0xD800 && this._first <= 0xDBFF && | ||
this._second >= 0xDC00 && this._second <= 0xDFFF); | ||
} | ||
else if (reference === SeekOrigin.End) { | ||
this._codePoint = undefined; | ||
this._c = undefined; | ||
this._index = this._length; | ||
this._first = -1; | ||
this._second = -1; | ||
this._isSurrogatePair = false; | ||
} | ||
if (count === 0) | ||
return; | ||
let n = 0; | ||
if (count > 0) { | ||
while (n < count && this.next()) { | ||
n++; | ||
} | ||
} | ||
else { | ||
count = -count; | ||
while (n < count && this.prev()) { | ||
n++; | ||
} | ||
} | ||
} | ||
} | ||
exports.StringWalker = StringWalker; | ||
/** | ||
* Defines the origin of a seek operation. | ||
*/ | ||
var SeekOrigin; | ||
(function (SeekOrigin) { | ||
/** | ||
* Seek relative to the start of the string. | ||
*/ | ||
SeekOrigin[SeekOrigin["Start"] = -1] = "Start"; | ||
/** | ||
* Seek relative to the current position. | ||
*/ | ||
SeekOrigin[SeekOrigin["Current"] = 0] = "Current"; | ||
/** | ||
* Seek relative to the end of the string. | ||
*/ | ||
SeekOrigin[SeekOrigin["End"] = 1] = "End"; | ||
})(SeekOrigin = exports.SeekOrigin || (exports.SeekOrigin = {})); | ||
//# sourceMappingURL=StringWalker.js.map |
{ | ||
"name": "@oozcitak/util", | ||
"version": "1.0.6", | ||
"version": "2.0.0", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "util", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50484
1059