Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "utfstring", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "UTF-safe string operations", | ||
@@ -15,2 +15,3 @@ "repository": { | ||
], | ||
"main": "utfstring", | ||
"author": "Cameron Dutro", | ||
@@ -23,4 +24,5 @@ "license": "MIT", | ||
"devDependencies": { | ||
"jasmine-node": "^1.14.5" | ||
"jasmine-node": "^1.14.5", | ||
"utfstring": "./" | ||
} | ||
} |
@@ -43,3 +43,3 @@ utfstring | ||
```javascript | ||
var UtfString = require('utfstring.js').UtfString; | ||
var UtfString = require('utfstring'); | ||
``` | ||
@@ -46,0 +46,0 @@ |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
@@ -1,2 +0,2 @@ | ||
var UtfString = require('../utfstring.js').UtfString; | ||
var UtfString = require('../utfstring.js'); | ||
@@ -3,0 +3,0 @@ describe('UtfString', function() { |
430
utfstring.js
(function() { | ||
var UtfString = { | ||
charAt: function(string, index) { | ||
var byteIndex = this._findCharacterByteIndex(string, index); | ||
var UtfString; | ||
if ((byteIndex < 0) || (byteIndex >= string.length)) { | ||
return ''; | ||
} | ||
if (typeof exports !== 'undefined' && exports !== null) { | ||
UtfString = exports; | ||
} else if (typeof window !== 'undefined' && window !== null) { | ||
if ((typeof window.UtfString === 'undefined') || (window.UtfString === null)) { | ||
window.UtfString = {}; | ||
} | ||
var characters = string.slice(byteIndex, byteIndex + 8); | ||
var match = unsupportedPairs.exec(characters); | ||
UtfString = window.UtfString; | ||
} | ||
if (match === null) { | ||
return characters[0]; | ||
} else { | ||
return match[0]; | ||
} | ||
}, | ||
UtfString.charAt = function(string, index) { | ||
var byteIndex = findCharacterByteIndex(string, index); | ||
charCodeAt: function(string, index) { | ||
var byteIndex = this._findSurrogateByteIndex(string, index); | ||
if ((byteIndex < 0) || (byteIndex >= string.length)) { | ||
return ''; | ||
} | ||
if (byteIndex < 0) { | ||
return NaN; | ||
} | ||
var characters = string.slice(byteIndex, byteIndex + 8); | ||
var match = unsupportedPairs.exec(characters); | ||
var code = string.charCodeAt(byteIndex); | ||
if (match === null) { | ||
return characters[0]; | ||
} else { | ||
return match[0]; | ||
} | ||
} | ||
if ((0xD800 <= code) && (code <= 0xDBFF)) { | ||
var hi = code; | ||
var low = string.charCodeAt(byteIndex + 1); | ||
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000; | ||
} | ||
UtfString.charCodeAt = function(string, index) { | ||
var byteIndex = findSurrogateByteIndex(string, index); | ||
return code; | ||
}, | ||
if (byteIndex < 0) { | ||
return NaN; | ||
} | ||
fromCharCode: function(charCode) { | ||
if (charCode > 0xFFFF) { | ||
charCode -= 0x10000; | ||
var code = string.charCodeAt(byteIndex); | ||
return String.fromCharCode( | ||
0xD800 + (charCode >> 10), 0xDC00 + (charCode & 0x3FF) | ||
); | ||
} else { | ||
return String.fromCharCode(charCode); | ||
} | ||
}, | ||
if ((0xD800 <= code) && (code <= 0xDBFF)) { | ||
var hi = code; | ||
var low = string.charCodeAt(byteIndex + 1); | ||
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000; | ||
} | ||
indexOf: function(string, searchValue, start) { | ||
if ((typeof start === 'undefined') || (start === null)) { | ||
start = 0; | ||
} | ||
return code; | ||
} | ||
var startByteIndex = this._findCharacterByteIndex(string, start); | ||
var index = string.indexOf(searchValue, startByteIndex); | ||
UtfString.fromCharCode = function(charCode) { | ||
if (charCode > 0xFFFF) { | ||
charCode -= 0x10000; | ||
if (index < 0) { | ||
return -1 | ||
} else { | ||
return this._findCharIndex(string, index); | ||
} | ||
}, | ||
return String.fromCharCode( | ||
0xD800 + (charCode >> 10), 0xDC00 + (charCode & 0x3FF) | ||
); | ||
} else { | ||
return String.fromCharCode(charCode); | ||
} | ||
} | ||
lastIndexOf: function(string, searchValue, start) { | ||
var index; | ||
UtfString.indexOf = function(string, searchValue, start) { | ||
if ((typeof start === 'undefined') || (start === null)) { | ||
start = 0; | ||
} | ||
if ((typeof start === 'undefined') || (start === null)) { | ||
index = string.lastIndexOf(searchValue); | ||
} else { | ||
var startByteIndex = this._findCharacterByteIndex(string, start); | ||
index = string.lastIndexOf(searchValue, startByteIndex); | ||
} | ||
var startByteIndex = findCharacterByteIndex(string, start); | ||
var index = string.indexOf(searchValue, startByteIndex); | ||
if (index < 0) { | ||
return -1; | ||
} else { | ||
return this._findCharIndex(string, index); | ||
} | ||
}, | ||
if (index < 0) { | ||
return -1 | ||
} else { | ||
return findCharIndex(string, index); | ||
} | ||
} | ||
slice: function(string, start, finish) { | ||
var startByteIndex = this._findCharacterByteIndex(string, start); | ||
var finishByteIndex; | ||
UtfString.lastIndexOf = function(string, searchValue, start) { | ||
var index; | ||
if (startByteIndex < 0) { | ||
startByteIndex = string.length; | ||
} | ||
if ((typeof start === 'undefined') || (start === null)) { | ||
index = string.lastIndexOf(searchValue); | ||
} else { | ||
var startByteIndex = findCharacterByteIndex(string, start); | ||
index = string.lastIndexOf(searchValue, startByteIndex); | ||
} | ||
if ((typeof finish === 'undefined') || (finish === null)) { | ||
finishByteIndex = string.length; | ||
} else { | ||
finishByteIndex = this._findCharacterByteIndex(string, finish); | ||
if (index < 0) { | ||
return -1; | ||
} else { | ||
return findCharIndex(string, index); | ||
} | ||
} | ||
if (finishByteIndex < 0) { | ||
finishByteIndex = string.length; | ||
} | ||
} | ||
UtfString.slice = function(string, start, finish) { | ||
var startByteIndex = findCharacterByteIndex(string, start); | ||
var finishByteIndex; | ||
return string.slice(startByteIndex, finishByteIndex); | ||
}, | ||
if (startByteIndex < 0) { | ||
startByteIndex = string.length; | ||
} | ||
substr: function(string, start, length) { | ||
if (start < 0) { | ||
start = this.length(string) + start; | ||
} | ||
if ((typeof finish === 'undefined') || (finish === null)) { | ||
finishByteIndex = string.length; | ||
} else { | ||
finishByteIndex = findCharacterByteIndex(string, finish); | ||
if ((typeof length === 'undefined') || (length === null)) { | ||
return this.slice(string, start); | ||
} else { | ||
return this.slice(string, start, start + length); | ||
if (finishByteIndex < 0) { | ||
finishByteIndex = string.length; | ||
} | ||
}, | ||
} | ||
// they do the same thing | ||
substring: this.slice, | ||
return string.slice(startByteIndex, finishByteIndex); | ||
} | ||
length: function(string) { | ||
return this._findCharIndex(string, string.length); | ||
}, | ||
UtfString.substr = function(string, start, length) { | ||
if (start < 0) { | ||
start = this.length(string) + start; | ||
} | ||
stringToCodePoints: function(string) { | ||
var result = []; | ||
if ((typeof length === 'undefined') || (length === null)) { | ||
return this.slice(string, start); | ||
} else { | ||
return this.slice(string, start, start + length); | ||
} | ||
} | ||
for (var i = 0; i < string.length; i ++) { | ||
codePoint = this.charCodeAt(string, i); | ||
// they do the same thing | ||
UtfString.substring = UtfString.slice | ||
if (!codePoint) { | ||
break; | ||
} | ||
UtfString.length = function(string) { | ||
return findCharIndex(string, string.length); | ||
} | ||
result.push(codePoint); | ||
} | ||
UtfString.stringToCodePoints = function(string) { | ||
var result = []; | ||
return result; | ||
}, | ||
for (var i = 0; i < string.length; i ++) { | ||
codePoint = this.charCodeAt(string, i); | ||
codePointsToString: function(arr) { | ||
var chars = []; | ||
for (var i = 0; i < arr.length; i ++) { | ||
chars.push(this.fromCharCode(arr[i])); | ||
if (!codePoint) { | ||
break; | ||
} | ||
return chars.join(''); | ||
}, | ||
result.push(codePoint); | ||
} | ||
stringToBytes: function(string) { | ||
var result = []; | ||
return result; | ||
} | ||
for (var i = 0; i < string.length; i ++) { | ||
var chr = string.charCodeAt(i); | ||
var byteArray = []; | ||
UtfString.codePointsToString = function(arr) { | ||
var chars = []; | ||
while (chr > 0) { | ||
byteArray.push(chr & 0xFF); | ||
chr >>= 8; | ||
} | ||
for (var i = 0; i < arr.length; i ++) { | ||
chars.push(this.fromCharCode(arr[i])); | ||
} | ||
// all utf-16 characters are two bytes | ||
if (byteArray.length == 1) { | ||
byteArray.push(0); | ||
} | ||
return chars.join(''); | ||
} | ||
// assume big-endian | ||
result = result.concat(byteArray.reverse()); | ||
} | ||
UtfString.stringToBytes = function(string) { | ||
var result = []; | ||
return result; | ||
}, | ||
for (var i = 0; i < string.length; i ++) { | ||
var chr = string.charCodeAt(i); | ||
var byteArray = []; | ||
bytesToString: function(arr) { | ||
var result = []; | ||
while (chr > 0) { | ||
byteArray.push(chr & 0xFF); | ||
chr >>= 8; | ||
} | ||
for (var i = 0; i < arr.length; i += 2) { | ||
var hi = arr[i]; | ||
var low = arr[i + 1]; | ||
var combined = (hi << 8) | low; | ||
result.push(String.fromCharCode(combined)); | ||
// all utf-16 characters are two bytes | ||
if (byteArray.length == 1) { | ||
byteArray.push(0); | ||
} | ||
return result.join(''); | ||
}, | ||
// assume big-endian | ||
result = result.concat(byteArray.reverse()); | ||
} | ||
stringToCharArray: function(string) { | ||
var result = []; | ||
var regStr = unsupportedPairs.source + '|.'; | ||
var scanner = new RegExp(regStr, 'g'); | ||
return result; | ||
} | ||
do { | ||
var match = scanner.exec(string); | ||
UtfString.bytesToString = function(arr) { | ||
var result = []; | ||
if (match === null) { | ||
break; | ||
} | ||
for (var i = 0; i < arr.length; i += 2) { | ||
var hi = arr[i]; | ||
var low = arr[i + 1]; | ||
var combined = (hi << 8) | low; | ||
result.push(String.fromCharCode(combined)); | ||
} | ||
result.push(match[0]); | ||
} while(match !== null); | ||
return result.join(''); | ||
} | ||
return result; | ||
}, | ||
UtfString.stringToCharArray = function(string) { | ||
var result = []; | ||
var regStr = unsupportedPairs.source + '|.'; | ||
var scanner = new RegExp(regStr, 'g'); | ||
_findCharIndex: function(string, byteIndex) { | ||
// optimization: don't iterate unless necessary | ||
if (!this._containsUnsupportedCharacters(string)) { | ||
return byteIndex; | ||
do { | ||
var match = scanner.exec(string); | ||
if (match === null) { | ||
break; | ||
} | ||
var regStr = unsupportedPairs.source + '|.'; | ||
var scanner = new RegExp(regStr, 'g'); | ||
var charCount = 0; | ||
result.push(match[0]); | ||
} while(match !== null); | ||
while (scanner.exec(string) !== null) { | ||
if (scanner.lastIndex > byteIndex) { | ||
break; | ||
} | ||
return result; | ||
} | ||
charCount ++; | ||
function findCharIndex(string, byteIndex) { | ||
// optimization: don't iterate unless necessary | ||
if (!containsUnsupportedCharacters(string)) { | ||
return byteIndex; | ||
} | ||
var regStr = unsupportedPairs.source + '|.'; | ||
var scanner = new RegExp(regStr, 'g'); | ||
var charCount = 0; | ||
while (scanner.exec(string) !== null) { | ||
if (scanner.lastIndex > byteIndex) { | ||
break; | ||
} | ||
return charCount; | ||
}, | ||
charCount ++; | ||
} | ||
_findCharacterByteIndex: function(string, charIndex) { | ||
return this._scan(string, this._createScanner(), charIndex); | ||
}, | ||
return charCount; | ||
} | ||
_findSurrogateByteIndex: function(string, charIndex) { | ||
return this._scan(string, new RegExp(surrogatePairs.source, 'g'), charIndex); | ||
}, | ||
function findCharacterByteIndex(string, charIndex) { | ||
return scan(string, createScanner(), charIndex); | ||
} | ||
_scan: function(string, scanner, charIndex) { | ||
// optimization: don't iterate unless it's necessary | ||
if (!this._containsUnsupportedCharacters(string)) { | ||
return charIndex; | ||
} | ||
function findSurrogateByteIndex(string, charIndex) { | ||
return scan(string, new RegExp(surrogatePairs.source, 'g'), charIndex); | ||
} | ||
var byteIndex = 0; | ||
var charCount = 0; | ||
function scan(string, scanner, charIndex) { | ||
// optimization: don't iterate unless it's necessary | ||
if (!containsUnsupportedCharacters(string)) { | ||
return charIndex; | ||
} | ||
do { | ||
var match = scanner.exec(string); | ||
var byteIndex = 0; | ||
var charCount = 0; | ||
if (match === null) { | ||
break; | ||
} | ||
do { | ||
var match = scanner.exec(string); | ||
if (charCount < charIndex) { | ||
byteIndex += match[0].length; | ||
charCount ++; | ||
} else { | ||
break; | ||
} | ||
} while (match !== null); | ||
if (match === null) { | ||
break; | ||
} | ||
if (byteIndex >= string.length) { | ||
return -1; | ||
if (charCount < charIndex) { | ||
byteIndex += match[0].length; | ||
charCount ++; | ||
} else { | ||
break; | ||
} | ||
} while (match !== null); | ||
return byteIndex; | ||
}, | ||
if (byteIndex >= string.length) { | ||
return -1; | ||
} | ||
_containsUnsupportedCharacters: function(string) { | ||
return unsupportedPairs.test(string); | ||
}, | ||
return byteIndex; | ||
} | ||
_createScanner: function(modifiers) { | ||
if ((typeof modifiers === 'undefined') || (modifiers === null)) { | ||
modifiers = ''; | ||
} | ||
function containsUnsupportedCharacters(string) { | ||
return unsupportedPairs.test(string); | ||
} | ||
var regStr = [regionalIndicatorPairs.source, surrogatePairs.source].join('|'); | ||
return new RegExp(regStr, modifiers); | ||
function createScanner(modifiers) { | ||
if ((typeof modifiers === 'undefined') || (modifiers === null)) { | ||
modifiers = ''; | ||
} | ||
}; | ||
var regStr = [regionalIndicatorPairs.source, surrogatePairs.source].join('|'); | ||
return new RegExp(regStr, modifiers); | ||
} | ||
var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/; | ||
var regionalIndicatorPairs = /\uD83C[\uDDE6-\uDDFF]\uD83C[\uDDE6-\uDDFF]/; | ||
var unsupportedPairs = UtfString._createScanner(); | ||
var root; | ||
if (typeof exports !== 'undefined' && exports !== null) { | ||
root = exports; | ||
} else if (typeof window !== 'undefined' && window !== null) { | ||
root = window; | ||
} | ||
root.UtfString = UtfString; | ||
var unsupportedPairs = createScanner(); | ||
})(); |
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
82586
37
1570
2