mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2025-01-07 15:49:23 +03:00
Return as soon as first paired character is found
Previously the character count of the entire string was taken even though it was only checking for the presence of a paired character. Now hasPairedCharacter returns as early as possible and the now unused getCharacterCount has been removed.
This commit is contained in:
parent
e343b0e0fc
commit
f1fd13b0b2
@ -1,17 +1,6 @@
|
||||
textUtils = require '../src/text-utils'
|
||||
|
||||
describe 'text utilities', ->
|
||||
describe '.getCharacterCount(string)', ->
|
||||
it 'returns the number of full characters in the string', ->
|
||||
expect(textUtils.getCharacterCount('abc')).toBe 3
|
||||
expect(textUtils.getCharacterCount('a\uD835\uDF97b\uD835\uDF97c')).toBe 5
|
||||
expect(textUtils.getCharacterCount('\uD835\uDF97')).toBe 1
|
||||
expect(textUtils.getCharacterCount('\u2714\uFE0E')).toBe 1
|
||||
expect(textUtils.getCharacterCount('\uD835')).toBe 1
|
||||
expect(textUtils.getCharacterCount('\uDF97')).toBe 1
|
||||
expect(textUtils.getCharacterCount('\uFE0E')).toBe 1
|
||||
expect(textUtils.getCharacterCount('\uFE0E\uFE0E')).toBe 2
|
||||
|
||||
describe '.hasPairedCharacter(string)', ->
|
||||
it 'returns true when the string contains a surrogate pair or variation sequence', ->
|
||||
expect(textUtils.hasPairedCharacter('abc')).toBe false
|
||||
|
@ -36,27 +36,16 @@ isVariationSequence = (string, index=0) ->
|
||||
isPairedCharacter = (string, index=0) ->
|
||||
isSurrogatePair(string, index) or isVariationSequence(string, index)
|
||||
|
||||
# Get the number of characters in the string accounting for surrogate pairs and
|
||||
# variation sequences.
|
||||
#
|
||||
# This method counts high/low surrogate pairs and variation sequences as a
|
||||
# single character and 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
|
||||
count-- for index in [0...string.length] when isPairedCharacter(string, index)
|
||||
count
|
||||
|
||||
# Does the given string contain at least surrogate pair or variation sequence?
|
||||
#
|
||||
# * `string` The {String} to check for the presence of paired characters.
|
||||
#
|
||||
# Returns a {Boolean}.
|
||||
hasPairedCharacter = (string) ->
|
||||
string.length isnt getCharacterCount(string)
|
||||
index = 0
|
||||
while index < string.length
|
||||
return true if isPairedCharacter(string, index)
|
||||
index++
|
||||
false
|
||||
|
||||
module.exports = {getCharacterCount, isPairedCharacter, hasPairedCharacter}
|
||||
module.exports = {isPairedCharacter, hasPairedCharacter}
|
||||
|
Loading…
Reference in New Issue
Block a user