Doc text-utils.coffee

This commit is contained in:
Kevin Sawicki
2013-06-04 12:03:06 -07:00
parent a109a3317e
commit 06a5234d10

View File

@@ -1,18 +1,41 @@
### Internal ###
isHighSurrogate = (string, index) ->
0xD800 <= string.charCodeAt(index) <= 0xDBFF
isLowSurrogate = (string, index) ->
0xDC00 <= string.charCodeAt(index) <= 0xDFFF
### Public ###
# Is the character at the given index the start of a high/low
# surrogate pair?
#
# string - The {String} to check for a surrogate pair.
# index - The {Number} index to look for a surrogate pair at.
#
# Return a {Boolean}.
isSurrogatePair = (string, index) ->
isHighSurrogate(string, index) and isLowSurrogate(string, index + 1)
# Get the number of characters in the string accounting for surrogate pairs.
#
# This will always returns a value less than or equal to `string.length`.
#
# string - The {String} to count the number of full characters in.
#
# Returns a {Number}.
getCharacterCount = (string) ->
count = string.length
for index in [0...string.length] when isSurrogatePair(string, index)
count--
count
# Does the given string contain at least one surrogate pair?
#
# string - The {String} to check for the presence of surrogate pairs.
#
# Returns a {Boolean}.
hasSurrogatePairs = (string) ->
string.length isnt getCharacterCount(string)