eastasianwidth
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -271,6 +271,12 @@ var eaw = {}; | ||
// Split a string considering surrogate-pairs. | ||
function stringToArray(string) { | ||
return string.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF]/g) || []; | ||
} | ||
eaw.length = function(string) { | ||
var characters = stringToArray(string); | ||
var len = 0; | ||
for (var i = 0; i < string.length; i++) { | ||
len = len + this.characterLength(string.charAt(i)); | ||
for (var i = 0; i < characters.length; i++) { | ||
len = len + this.characterLength(characters[i]); | ||
} | ||
@@ -281,13 +287,27 @@ return len; | ||
eaw.slice = function(text, start, end) { | ||
textLen = eaw.length(text) | ||
start = start ? start : 0; | ||
end = end ? end : 1; | ||
if (start < 0) { | ||
start = textLen + start; | ||
} | ||
if (end < 0) { | ||
end = textLen + end; | ||
} | ||
var result = ''; | ||
for (var i = 0; i < text.length; i++) { | ||
var char = text.charAt(i); | ||
var eawLen = eaw.length(result + char); | ||
if (eawLen >= 1 + start && eawLen < 1 + end) { | ||
result += char | ||
var eawLen = 0; | ||
var chars = stringToArray(text); | ||
for (var i = 0; i < chars.length; i++) { | ||
var char = chars[i]; | ||
var charLen = eaw.length(char); | ||
if (eawLen >= start - (charLen == 2 ? 1 : 0)) { | ||
if (eawLen + charLen <= end) { | ||
result += char; | ||
} else { | ||
break; | ||
} | ||
} | ||
eawLen += charLen; | ||
} | ||
return result; | ||
}; |
{ | ||
"name": "eastasianwidth", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Get East Asian Width from a character.", | ||
"main": "eastasianwidth.js", | ||
"files": [ | ||
"eastasianwidth.js" | ||
], | ||
"scripts": { | ||
@@ -7,0 +10,0 @@ "test": "mocha" |
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
13640
3
303