From a53eeafdc4b78ca4c47bdde34d8e1ea204e5f32b Mon Sep 17 00:00:00 2001 From: Omar Tawfik Date: Fri, 2 Jul 2021 02:03:50 -0700 Subject: [PATCH] Upgrade bindings to class syntax --- src/oniguruma.js | 206 ++++++++++++++++++++++++++--------------------- 1 file changed, 112 insertions(+), 94 deletions(-) diff --git a/src/oniguruma.js b/src/oniguruma.js index bf14317..c88dfcc 100644 --- a/src/oniguruma.js +++ b/src/oniguruma.js @@ -1,114 +1,132 @@ -'use strict' +'use strict'; -const OnigScanner = require('../build/Release/onig_scanner.node').OnigScanner -const OnigString = require('../build/Release/onig_scanner.node').OnigString +const OnigScanner = require('../build/Release/onig_scanner.node').OnigScanner; +const OnigString = require('../build/Release/onig_scanner.node').OnigString; -function OnigRegExp(source) { - this.source = source; - this.scanner = new OnigScanner([this.source]); -} - -OnigRegExp.prototype.captureIndicesForMatch = function(string, match) { - var capture, captureIndices, i, len; - if (match != null) { - captureIndices = match.captureIndices; - string = this.scanner.convertToString(string); - for (i = 0, len = captureIndices.length; i < len; i++) { - capture = captureIndices[i]; - capture.match = string.slice(capture.start, capture.end); - } - return captureIndices; - } else { - return null; +class OnigStringImpl extends OnigString { + substring(start, end) { + return this.content.substring(start, end); } -}; -OnigRegExp.prototype.searchSync = function(string, startPosition) { - var match; - if (startPosition == null) { - startPosition = 0; + toString(start, end) { + return this.content; } - match = this.scanner.findNextMatchSync(string, startPosition); - return this.captureIndicesForMatch(string, match); -}; -OnigRegExp.prototype.search = function(string, startPosition, callback) { - if (startPosition == null) { - startPosition = 0; + get length() { + return this.content.length; } - if (typeof startPosition === 'function') { - callback = startPosition; - startPosition = 0; +} + +class OnigScannerImpl extends OnigScanner { + findNextMatch(string, startPosition, callback) { + if (startPosition == null) startPosition = 0; + if (typeof startPosition === 'function') { + callback = startPosition; + startPosition = 0; + } + + string = this.convertToString(string); + startPosition = this.convertToNumber(startPosition); + + this._findNextMatch(string, startPosition, (error, match) => { + if (match) match.scanner = this; + return callback(error, match); + }); } - return this.scanner.findNextMatch(string, startPosition, (function(_this) { - return function(error, match) { - return typeof callback === "function" ? callback(error, _this.captureIndicesForMatch(string, match)) : void 0; - }; - })(this)); -}; - -OnigRegExp.prototype.testSync = function(string) { - return this.searchSync(string) != null; -}; - -OnigRegExp.prototype.test = function(string, callback) { - return this.search(string, 0, function(error, result) { - return typeof callback === "function" ? callback(error, result != null) : void 0; - }); -}; - -OnigScanner.prototype.findNextMatch = function (string, startPosition, callback) { - if (startPosition == null) startPosition = 0 - if (typeof startPosition === 'function') { - callback = startPosition - startPosition = 0 + + findNextMatchSync(string, startPosition) { + if (startPosition == null) { + startPosition = 0; + } + string = this.convertToString(string); + startPosition = this.convertToNumber(startPosition); + + let match = this._findNextMatchSync(string, startPosition); + if (match) match.scanner = this; + return match; } - string = this.convertToString(string) - startPosition = this.convertToNumber(startPosition) + convertToString(value) { + if (value === undefined) return 'undefined'; + if (value === null) return 'null'; + if (value instanceof OnigStringImpl) return value; + return value.toString(); + } - this._findNextMatch(string, startPosition, (error, match) => { - if (match) match.scanner = this - return callback(error, match) - }) + convertToNumber(value) { + value = parseInt(value); + if (!isFinite(value)) { + value = 0; + } + value = Math.max(value, 0); + return value; + } } -OnigScanner.prototype.findNextMatchSync = function (string, startPosition) { - if (startPosition == null) { startPosition = 0 } - string = this.convertToString(string) - startPosition = this.convertToNumber(startPosition) +class OnigRegExpImpl { + constructor(source) { + this.source = source; + this.scanner = new OnigScannerImpl([this.source]); + } - let match = this._findNextMatchSync(string, startPosition) - if (match) match.scanner = this - return match -} + captureIndicesForMatch(string, match) { + var capture, captureIndices, i, len; + if (match != null) { + captureIndices = match.captureIndices; + string = this.scanner.convertToString(string); + for (i = 0, len = captureIndices.length; i < len; i++) { + capture = captureIndices[i]; + capture.match = string.slice(capture.start, capture.end); + } + return captureIndices; + } else { + return null; + } + } -OnigScanner.prototype.convertToString = function (value) { - if (value === undefined) return 'undefined' - if (value === null) return 'null' - if (value.constructor == OnigString) return value - return value.toString() -} + searchSync(string, startPosition) { + var match; + if (startPosition == null) { + startPosition = 0; + } + match = this.scanner.findNextMatchSync(string, startPosition); + return this.captureIndicesForMatch(string, match); + } -OnigScanner.prototype.convertToNumber = function (value) { - value = parseInt(value) - if (!isFinite(value)) { value = 0 } - value = Math.max(value, 0) - return value -} + search(string, startPosition, callback) { + if (startPosition == null) { + startPosition = 0; + } + if (typeof startPosition === 'function') { + callback = startPosition; + startPosition = 0; + } + return this.scanner.findNextMatch( + string, + startPosition, + (function (_this) { + return function (error, match) { + return typeof callback === 'function' + ? callback(error, _this.captureIndicesForMatch(string, match)) + : void 0; + }; + })(this) + ); + } -OnigString.prototype.substring = function (start, end) { - return this.content.substring(start, end) -} + testSync(string) { + return this.searchSync(string) != null; + } -OnigString.prototype.toString = function (start, end) { - return this.content + test(string, callback) { + return this.search(string, 0, function (error, result) { + return typeof callback === 'function' + ? callback(error, result != null) + : void 0; + }); + } } -Object.defineProperty(OnigString.prototype, 'length', { - get() { return this.content.length } -}) - -exports.OnigScanner = OnigScanner -exports.OnigRegExp = OnigRegExp -exports.OnigString = OnigString +exports.OnigString = OnigStringImpl; +exports.OnigScanner = OnigScannerImpl; +exports.OnigRegExp = OnigRegExpImpl;