@oozcitak/util
Advanced tools
Comparing version 8.3.6 to 8.3.7
@@ -5,3 +5,3 @@ export { FixedSizeSet } from './FixedSizeSet'; | ||
export { Lazy } from './Lazy'; | ||
export { StringWalker, SeekOrigin } from './StringWalker'; | ||
export { StringWalker } from './StringWalker'; | ||
/** | ||
@@ -8,0 +8,0 @@ * Applies the mixin to a given class. |
@@ -24,3 +24,2 @@ "use strict"; | ||
exports.StringWalker = StringWalker_1.StringWalker; | ||
exports.SeekOrigin = StringWalker_1.SeekOrigin; | ||
/** | ||
@@ -27,0 +26,0 @@ * Applies the mixin to a given class. |
/** | ||
* Walks the code points of a string. | ||
* Walks through the code points of a string. | ||
*/ | ||
export declare class StringWalker { | ||
private _chars; | ||
private _index; | ||
private _length; | ||
private _c?; | ||
private _codePoint?; | ||
private _isSurrogatePair; | ||
private _first; | ||
private _second; | ||
private _pointer; | ||
private _codePoint; | ||
private _c; | ||
private _remaining; | ||
private _substring; | ||
/** | ||
@@ -24,103 +23,28 @@ * Initializes a new `StringWalker`. | ||
/** | ||
* Returns the current code point. Returns `-1` if the current position is | ||
* beyond the end of string. | ||
* Returns the number of code points in the input string. | ||
*/ | ||
get codePoint(): number; | ||
get length(): number; | ||
/** | ||
* Returns the current character. Returns an empty string if the current | ||
* position is beyond the end of string. | ||
* Returns the current code point. Returns `-1` if the position is beyond | ||
* the end of string. | ||
*/ | ||
get c(): string; | ||
codePoint(): number; | ||
/** | ||
* Returns the substring including the current character without changing | ||
* the current position. | ||
* | ||
* @param count - the number of code points to return | ||
* Returns the current character. Returns an empty string if the position is | ||
* beyond the end of string. | ||
*/ | ||
peek(count?: number): string; | ||
c(): string; | ||
/** | ||
* Returns the substring including the current character without changing | ||
* the current position. Returns character points instead of code points. | ||
* | ||
* @param charCount - the number of character codes to return | ||
* Returns the remaining string. | ||
*/ | ||
peekChar(charCount?: number): string; | ||
remaining(): string; | ||
/** | ||
* Determines whether the substring including the current character | ||
* starts with the given string. | ||
* | ||
* @param match - the string to match | ||
* Returns the substring from the current character to the end of string. | ||
*/ | ||
startsWith(match: string, ignoreCase?: boolean): boolean; | ||
substring(): string; | ||
/** | ||
* Moves to the next code point. | ||
* Gets or sets the current position. | ||
*/ | ||
next(): boolean; | ||
/** | ||
* Moves to the next character code. | ||
*/ | ||
nextChar(): boolean; | ||
/** | ||
* Moves to the previous code point. | ||
*/ | ||
prev(): boolean; | ||
/** | ||
* Moves to the previous character code. | ||
*/ | ||
prevChar(): boolean; | ||
/** | ||
* 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: number, reference?: SeekOrigin): void; | ||
/** | ||
* Seeks a number of character codes relative to the current position. | ||
* | ||
* @param charCount - the number of character codes to return | ||
* @param reference - reference of the seek operation | ||
*/ | ||
seekChar(charCount: number, reference?: SeekOrigin): void; | ||
/** | ||
* Consumes a number of code points. | ||
* | ||
* @param count - number of code points to take | ||
*/ | ||
take(countOrFunc: number | ((char: string) => boolean)): string; | ||
/** | ||
* Consumes a number of character codes. | ||
* | ||
* @param countOrFunc - the number of character codes to take | ||
*/ | ||
takeChar(countOrFunc: number | ((char: string) => boolean)): string; | ||
/** | ||
* Skips a number of code points. | ||
* | ||
* @param count - number of code points to skip | ||
*/ | ||
skip(countOrFunc: number | ((char: string) => boolean)): void; | ||
/** | ||
* Skips a number of character codes. | ||
* | ||
* @param countOrFunc - the number of character codes to skip | ||
*/ | ||
skipChar(countOrFunc: number | ((char: string) => boolean)): void; | ||
get pointer(): number; | ||
set pointer(val: number); | ||
} | ||
/** | ||
* Defines the origin of a seek operation. | ||
*/ | ||
export declare enum SeekOrigin { | ||
/** | ||
* Seek relative to the start of the string. | ||
*/ | ||
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 }); | ||
var _1 = require("."); | ||
/** | ||
* Walks the code points of a string. | ||
* Walks through the code points of a string. | ||
*/ | ||
@@ -14,12 +13,5 @@ var StringWalker = /** @class */ (function () { | ||
function StringWalker(input) { | ||
this._isSurrogatePair = false; | ||
this._first = -1; | ||
this._second = -1; | ||
this._chars = input; | ||
this._index = 0; | ||
this._pointer = 0; | ||
this._chars = Array.from(input); | ||
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); | ||
} | ||
@@ -30,349 +22,86 @@ Object.defineProperty(StringWalker.prototype, "eof", { | ||
*/ | ||
get: function () { return this._index >= this._length; }, | ||
get: function () { return this._pointer >= this._length; }, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(StringWalker.prototype, "codePoint", { | ||
Object.defineProperty(StringWalker.prototype, "length", { | ||
/** | ||
* Returns the current code point. Returns `-1` if the current position is | ||
* beyond the end of string. | ||
* Returns the number of code points in the input string. | ||
*/ | ||
get: function () { | ||
if (this._codePoint === undefined) { | ||
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 { | ||
this._codePoint = this._first; | ||
} | ||
} | ||
return this._codePoint; | ||
}, | ||
get: function () { return this._length; }, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(StringWalker.prototype, "c", { | ||
/** | ||
* Returns the current character. Returns an empty string if the current | ||
* position is beyond the end of string. | ||
*/ | ||
get: function () { | ||
if (this._c === undefined) { | ||
var cp = this.codePoint; | ||
this._c = (cp === -1 ? "" : String.fromCodePoint(cp)); | ||
} | ||
return this._c; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
/** | ||
* Returns the substring including the current character without changing | ||
* the current position. | ||
* | ||
* @param count - the number of code points to return | ||
* Returns the current code point. Returns `-1` if the position is beyond | ||
* the end of string. | ||
*/ | ||
StringWalker.prototype.peek = function (count) { | ||
if (this.eof) | ||
return ""; | ||
if (count === undefined) | ||
return this._chars.slice(this._index); | ||
var originalIndex = this._index; | ||
var charCount = 0; | ||
var n = 0; | ||
while (n < count) { | ||
charCount += this._isSurrogatePair ? 2 : 1; | ||
n++; | ||
if (!this.next()) | ||
break; | ||
} | ||
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 including the current character without changing | ||
* the current position. Returns character points instead of code points. | ||
* | ||
* @param charCount - the number of character codes to return | ||
*/ | ||
StringWalker.prototype.peekChar = function (charCount) { | ||
if (this.eof) | ||
return ""; | ||
if (charCount === undefined) | ||
return this._chars.slice(this._index); | ||
return this._chars.slice(this._index, this._index + charCount); | ||
}; | ||
/** | ||
* Determines whether the substring including the current character | ||
* starts with the given string. | ||
* | ||
* @param match - the string to match | ||
*/ | ||
StringWalker.prototype.startsWith = function (match, ignoreCase) { | ||
if (ignoreCase === void 0) { ignoreCase = false; } | ||
if (this.eof) | ||
return false; | ||
var len = match.length; | ||
if (len > this._length - this._index) | ||
return false; | ||
if (ignoreCase) { | ||
var chars = this._chars.slice(this._index, this._index + match.length); | ||
if (match.toLowerCase() !== chars.toLowerCase()) | ||
return false; | ||
} | ||
else { | ||
for (var i = 0; i < len; i++) { | ||
if (match[i] !== this._chars[this._index + i]) | ||
return false; | ||
StringWalker.prototype.codePoint = function () { | ||
if (this._codePoint === undefined) { | ||
if (this.eof) { | ||
this._codePoint = -1; | ||
} | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Moves to the next code point. | ||
*/ | ||
StringWalker.prototype.next = function () { | ||
if (this.eof) | ||
return false; | ||
this._codePoint = undefined; | ||
this._c = 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 next character code. | ||
*/ | ||
StringWalker.prototype.nextChar = function () { | ||
if (this.eof) | ||
return false; | ||
this._codePoint = undefined; | ||
this._c = undefined; | ||
this._index++; | ||
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. | ||
*/ | ||
StringWalker.prototype.prev = function () { | ||
if (this._index === 0) | ||
return false; | ||
this._codePoint = undefined; | ||
this._c = undefined; | ||
var second = this._index > 0 ? this._chars.charCodeAt(this._index - 1) : -1; | ||
var 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; | ||
}; | ||
/** | ||
* Moves to the previous character code. | ||
*/ | ||
StringWalker.prototype.prevChar = function () { | ||
if (this._index === 0) | ||
return false; | ||
this._codePoint = undefined; | ||
this._c = undefined; | ||
this._index--; | ||
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; | ||
}; | ||
/** | ||
* 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 | ||
*/ | ||
StringWalker.prototype.seek = function (count, reference) { | ||
if (reference === void 0) { 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; | ||
var n = 0; | ||
if (count > 0) { | ||
while (n < count && this.next()) { | ||
n++; | ||
else { | ||
var cp = this._chars[this._pointer].codePointAt(0); | ||
/* istanbul ignore else */ | ||
if (cp !== undefined) { | ||
this._codePoint = cp; | ||
} | ||
else { | ||
this._codePoint = -1; | ||
} | ||
} | ||
} | ||
else { | ||
count = -count; | ||
while (n < count && this.prev()) { | ||
n++; | ||
} | ||
} | ||
return this._codePoint; | ||
}; | ||
/** | ||
* Seeks a number of character codes relative to the current position. | ||
* | ||
* @param charCount - the number of character codes to return | ||
* @param reference - reference of the seek operation | ||
* Returns the current character. Returns an empty string if the position is | ||
* beyond the end of string. | ||
*/ | ||
StringWalker.prototype.seekChar = function (charCount, reference) { | ||
if (reference === void 0) { reference = SeekOrigin.Current; } | ||
this._codePoint = undefined; | ||
this._c = undefined; | ||
if (reference === SeekOrigin.Start) { | ||
this._index = charCount; | ||
StringWalker.prototype.c = function () { | ||
if (this._c === undefined) { | ||
this._c = (this.eof ? "" : this._chars[this._pointer]); | ||
} | ||
else if (reference === SeekOrigin.End) { | ||
this._index = this._length - charCount; | ||
} | ||
else { | ||
this._index += charCount; | ||
} | ||
if (this._index < 0) | ||
this._index = 0; | ||
if (this._index > this._length) | ||
this._index = this._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); | ||
return this._c; | ||
}; | ||
/** | ||
* Consumes a number of code points. | ||
* | ||
* @param count - number of code points to take | ||
* Returns the remaining string. | ||
*/ | ||
StringWalker.prototype.take = function (countOrFunc) { | ||
if (_1.isNumber(countOrFunc)) { | ||
if (countOrFunc === 0) | ||
return ""; | ||
var str = ""; | ||
var n = 0; | ||
while (n < countOrFunc) { | ||
str += this.c; | ||
this.next(); | ||
n++; | ||
} | ||
return str; | ||
StringWalker.prototype.remaining = function () { | ||
if (this._remaining === undefined) { | ||
this._remaining = (this.eof ? | ||
"" : this._chars.slice(this._pointer + 1).join('')); | ||
} | ||
else { | ||
if (!countOrFunc(this.c)) | ||
return ""; | ||
var str = this.c; | ||
while (this.next() && countOrFunc(this.c)) { | ||
str += this.c; | ||
} | ||
return str; | ||
} | ||
return this._remaining; | ||
}; | ||
/** | ||
* Consumes a number of character codes. | ||
* | ||
* @param countOrFunc - the number of character codes to take | ||
* Returns the substring from the current character to the end of string. | ||
*/ | ||
StringWalker.prototype.takeChar = function (countOrFunc) { | ||
if (_1.isNumber(countOrFunc)) { | ||
if (countOrFunc === 0) | ||
return ""; | ||
var str = this._chars.slice(this._index, this._index + countOrFunc); | ||
this.seekChar(countOrFunc); | ||
return str; | ||
StringWalker.prototype.substring = function () { | ||
if (this._substring === undefined) { | ||
this._substring = (this.eof ? | ||
"" : this._chars.slice(this._pointer).join('')); | ||
} | ||
else { | ||
if (!countOrFunc(this.c)) | ||
return ""; | ||
var startIndex = this._index; | ||
while (this.nextChar() && countOrFunc(this.c)) { } | ||
return this._chars.slice(startIndex, this._index); | ||
} | ||
return this._substring; | ||
}; | ||
/** | ||
* Skips a number of code points. | ||
* | ||
* @param count - number of code points to skip | ||
*/ | ||
StringWalker.prototype.skip = function (countOrFunc) { | ||
if (_1.isNumber(countOrFunc)) { | ||
if (countOrFunc === 0) | ||
Object.defineProperty(StringWalker.prototype, "pointer", { | ||
/** | ||
* Gets or sets the current position. | ||
*/ | ||
get: function () { return this._pointer; }, | ||
set: function (val) { | ||
if (val === this._pointer) | ||
return; | ||
var n = 0; | ||
while (n < countOrFunc && this.next()) { | ||
n++; | ||
} | ||
} | ||
else { | ||
if (!countOrFunc(this.c)) | ||
return; | ||
while (this.next() && countOrFunc(this.c)) { } | ||
} | ||
}; | ||
/** | ||
* Skips a number of character codes. | ||
* | ||
* @param countOrFunc - the number of character codes to skip | ||
*/ | ||
StringWalker.prototype.skipChar = function (countOrFunc) { | ||
if (_1.isNumber(countOrFunc)) { | ||
if (countOrFunc === 0) | ||
return; | ||
this.seekChar(countOrFunc); | ||
} | ||
else { | ||
if (!countOrFunc(this.c)) | ||
return; | ||
while (this.nextChar() && countOrFunc(this.c)) { } | ||
} | ||
}; | ||
this._pointer = val; | ||
this._codePoint = undefined; | ||
this._c = undefined; | ||
this._remaining = undefined; | ||
this._substring = undefined; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return StringWalker; | ||
}()); | ||
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": "8.3.6", | ||
"version": "8.3.7", | ||
"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
61481
1398