fast-chunk-string
Advanced tools
Comparing version 1.0.0 to 1.0.1
35
index.js
@@ -7,3 +7,13 @@ 'use strict'; | ||
module.exports = (str, {size, unicodeAware = false}) => { | ||
const strLength = unicodeAware ? stringLength(str) : str.length; | ||
str = str || ''; | ||
if (!unicodeAware) { | ||
return getChunks(str, size); | ||
} | ||
return getChunksUnicode(str, size); | ||
}; | ||
function getChunks(str, size) { | ||
const strLength = str.length; | ||
const numChunks = Math.ceil(strLength / size); | ||
@@ -16,10 +26,21 @@ const chunks = new Array(numChunks); | ||
for (; i < numChunks; ++i, o += size) { | ||
if (unicodeAware) { | ||
chunks[i] = runes.substr(str, o, size); | ||
} else { | ||
chunks[i] = str.substr(o, size); | ||
} | ||
chunks[i] = str.substr(o, size); | ||
} | ||
return chunks; | ||
}; | ||
} | ||
function getChunksUnicode(str, size) { | ||
const strLength = stringLength(str); | ||
const numChunks = Math.ceil(strLength / size); | ||
const chunks = new Array(numChunks); | ||
let i = 0; | ||
let o = 0; | ||
for (; i < numChunks; ++i, o += size) { | ||
chunks[i] = runes.substr(str, o, size); | ||
} | ||
return chunks; | ||
} |
{ | ||
"name": "fast-chunk-string", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Chunk string into equal substrings with unicode support", | ||
@@ -33,2 +33,6 @@ "license": "MIT", | ||
], | ||
"dependencies": { | ||
"runes": "^0.4.3", | ||
"string-length": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -40,14 +44,8 @@ "benchmark": "^2.1.4", | ||
"fast-lorem-ipsum": "^1.1.0", | ||
"jest": "^21.2.1", | ||
"jest": "^22.0.4", | ||
"prettier": "^1.9.2" | ||
}, | ||
"jest": { | ||
"modulePathIgnorePatterns": [ | ||
"component-test" | ||
] | ||
}, | ||
"dependencies": { | ||
"runes": "^0.4.3", | ||
"string-length": "^2.0.0" | ||
"testEnvironment": "node" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# fast-chunk-string [](https://travis-ci.org/vladgolubev/fast-chunk-string) | ||
# fast-chunk-string [](https://circleci.com/gh/vladgolubev/fast-chunk-string) | ||
@@ -3,0 +3,0 @@ > Chunk string into equal substrings with unicode support |
4800
32