Skip to content

Commit be37c13

Browse files
committed
Add in grapheme-splitter library
This is used to make sure that ElixirScript correctly splits unicode characters
1 parent f35480a commit be37c13

7 files changed

Lines changed: 80 additions & 9 deletions

File tree

lib/elixir_script/lib/functions.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ defmodule ElixirScript.Core.Functions do
33
use ElixirScript.FFI, global: true
44

55
defexternal split_at(value, position)
6+
7+
defexternal graphemes(str)
68
end

lib/elixir_script/lib/string.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ defmodule ElixirScript.String do
8686
end
8787

8888
def graphemes(str) do
89-
str.split('')
89+
ElixirScript.Core.Functions.graphemes(str)
9090
end
9191

9292
def length(str) do

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
"license": "MIT",
2222
"dependencies": {
2323
"erlang-types": "^1.0.1",
24+
"grapheme-splitter": "^1.0.2",
25+
"rollup-plugin-commonjs": "^8.2.1",
2426
"tailored": "^2.7.2"
2527
},
2628
"devDependencies": {
27-
"@std/esm": "^0.5.1",
29+
"@std/esm": "^0.8.3",
2830
"ava": "^0.21.0",
2931
"babel-core": "^6.24.0",
3032
"babel-preset-env": "^1.6.0",
@@ -40,7 +42,9 @@
4042
"sinon": "^2.4.1"
4143
},
4244
"ava": {
43-
"require": ["babel-register"],
45+
"require": [
46+
"babel-register"
47+
],
4448
"babel": {
4549
"babelrc": true
4650
}

rollup.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import nodeResolve from 'rollup-plugin-node-resolve';
2+
import commonjs from 'rollup-plugin-commonjs';
23
import babel from 'rollup-plugin-babel';
34
import minify from 'rollup-plugin-babel-minify';
45

56
export default {
67
entry: 'src/javascript/elixir.js',
78
moduleName: 'ElixirScript',
89
plugins: [
9-
nodeResolve({ jsnext: true }),
10+
nodeResolve({
11+
jsnext: true,
12+
main: true,
13+
}),
14+
commonjs(),
1015
babel({
1116
babelrc: false,
1217
}),

src/javascript/lib/core/functions.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import GraphemeSplitter from 'grapheme-splitter';
12
import Protocol from './protocol';
23
import Core from '../core';
34
import proplists from './erlang_compat/proplists';
@@ -163,8 +164,11 @@ function trampoline(f) {
163164
}
164165

165166
function split_at(value, position) {
167+
const splitter = new GraphemeSplitter();
168+
const splitValues = splitter.splitGraphemes(value);
169+
166170
if (position < 0) {
167-
const newPosition = value.length + position;
171+
const newPosition = splitValues.length + position;
168172
if (newPosition < 0) {
169173
return new Core.Tuple('', value);
170174
}
@@ -176,7 +180,7 @@ function split_at(value, position) {
176180
let second = '';
177181
let index = 0;
178182

179-
for (const character of value) {
183+
for (const character of splitValues) {
180184
if (index < position) {
181185
first += character;
182186
} else {
@@ -189,6 +193,11 @@ function split_at(value, position) {
189193
return new Core.Tuple(first, second);
190194
}
191195

196+
function graphemes(str) {
197+
const splitter = new GraphemeSplitter();
198+
return splitter.splitGraphemes(str);
199+
}
200+
192201
export default {
193202
call_property,
194203
defprotocol,
@@ -199,4 +208,5 @@ export default {
199208
trampoline,
200209
Recurse,
201210
split_at,
211+
graphemes,
202212
};

src/javascript/tests/core/functions.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ test('split_at', (t) => {
2121
t.deepEqual(Functions.split_at('abc', 1000).values, ['abc', '']);
2222
t.deepEqual(Functions.split_at('abc', -1000).values, ['', 'abc']);
2323
t.deepEqual(Functions.split_at('😀abélkm', 4).values, ['😀abé', 'lkm']);
24+
t.deepEqual(Functions.split_at('👨‍👩‍👦‍👦abélkm', 4).values, ['👨‍👩‍👦‍👦abé', 'lkm']);
2425
});
2526

2627
test('map_to_object/1', (t) => {

yarn.lock

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
dependencies:
4949
arrify "^1.0.1"
5050

51-
"@std/esm@^0.5.1":
52-
version "0.5.1"
53-
resolved "https://registry.yarnpkg.com/@std/esm/-/esm-0.5.1.tgz#5d9c855c1ce68401bd235b835a4fc609b1a8df26"
51+
"@std/esm@^0.8.3":
52+
version "0.8.3"
53+
resolved "https://registry.yarnpkg.com/@std/esm/-/esm-0.8.3.tgz#7a3c3144ca0905c9b92d82b79987503ecb58a193"
5454

5555
abbrev@1:
5656
version "1.1.0"
@@ -70,6 +70,10 @@ acorn@^5.0.1:
7070
version "5.0.3"
7171
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.0.3.tgz#c460df08491463f028ccb82eab3730bf01087b3d"
7272

73+
acorn@^5.1.1:
74+
version "5.1.2"
75+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.1.2.tgz#911cb53e036807cf0fa778dc5d370fbd864246d7"
76+
7377
ajv-keywords@^1.0.0:
7478
version "1.5.1"
7579
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.5.1.tgz#314dd0a4b3368fad3dfcdc54ede6171b886daf3c"
@@ -1664,6 +1668,14 @@ estree-walker@^0.2.1:
16641668
version "0.2.1"
16651669
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.2.1.tgz#bdafe8095383d8414d5dc2ecf4c9173b6db9412e"
16661670

1671+
estree-walker@^0.3.0:
1672+
version "0.3.1"
1673+
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.3.1.tgz#e6b1a51cf7292524e7237c312e5fe6660c1ce1aa"
1674+
1675+
estree-walker@^0.5.0:
1676+
version "0.5.0"
1677+
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.5.0.tgz#aae3b57c42deb8010e349c892462f0e71c5dd1aa"
1678+
16671679
esutils@^2.0.2:
16681680
version "2.0.2"
16691681
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
@@ -1992,6 +2004,10 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2:
19922004
version "4.1.11"
19932005
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
19942006

2007+
grapheme-splitter@^1.0.2:
2008+
version "1.0.2"
2009+
resolved "https://registry.yarnpkg.com/grapheme-splitter/-/grapheme-splitter-1.0.2.tgz#639e9dc1bf065892c643de31daa27cf58b1068e2"
2010+
19952011
handlebars@^4.0.3:
19962012
version "4.0.10"
19972013
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.10.tgz#3d30c718b09a3d96f23ea4cc1f403c4d3ba9ff4f"
@@ -2629,6 +2645,12 @@ lru-cache@^4.0.0, lru-cache@^4.0.1:
26292645
pseudomap "^1.0.2"
26302646
yallist "^2.1.2"
26312647

2648+
magic-string@^0.22.4:
2649+
version "0.22.4"
2650+
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.22.4.tgz#31039b4e40366395618c1d6cf8193c53917475ff"
2651+
dependencies:
2652+
vlq "^0.2.1"
2653+
26322654
make-dir@^1.0.0:
26332655
version "1.0.0"
26342656
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-1.0.0.tgz#97a011751e91dd87cfadef58832ebb04936de978"
@@ -3389,6 +3411,12 @@ resolve@^1.1.6, resolve@^1.2.0:
33893411
dependencies:
33903412
path-parse "^1.0.5"
33913413

3414+
resolve@^1.4.0:
3415+
version "1.4.0"
3416+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.4.0.tgz#a75be01c53da25d934a98ebd0e4c4a7312f92a86"
3417+
dependencies:
3418+
path-parse "^1.0.5"
3419+
33923420
restore-cursor@^2.0.0:
33933421
version "2.0.0"
33943422
resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf"
@@ -3425,6 +3453,16 @@ rollup-plugin-babel@^2.7.1:
34253453
object-assign "^4.1.0"
34263454
rollup-pluginutils "^1.5.0"
34273455

3456+
rollup-plugin-commonjs@^8.2.1:
3457+
version "8.2.1"
3458+
resolved "https://registry.yarnpkg.com/rollup-plugin-commonjs/-/rollup-plugin-commonjs-8.2.1.tgz#5e40c78375eb163c14c76bce69da1750e5905a2e"
3459+
dependencies:
3460+
acorn "^5.1.1"
3461+
estree-walker "^0.5.0"
3462+
magic-string "^0.22.4"
3463+
resolve "^1.4.0"
3464+
rollup-pluginutils "^2.0.1"
3465+
34283466
rollup-plugin-node-resolve@^3.0.0:
34293467
version "3.0.0"
34303468
resolved "https://registry.yarnpkg.com/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-3.0.0.tgz#8b897c4c3030d5001277b0514b25d2ca09683ee0"
@@ -3441,6 +3479,13 @@ rollup-pluginutils@^1.5.0:
34413479
estree-walker "^0.2.1"
34423480
minimatch "^3.0.2"
34433481

3482+
rollup-pluginutils@^2.0.1:
3483+
version "2.0.1"
3484+
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.0.1.tgz#7ec95b3573f6543a46a6461bd9a7c544525d0fc0"
3485+
dependencies:
3486+
estree-walker "^0.3.0"
3487+
micromatch "^2.3.11"
3488+
34443489
rollup@^0.45.2:
34453490
version "0.45.2"
34463491
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.45.2.tgz#63a284c2b31234656f24e9e9717fabb6a7f0fa43"
@@ -3930,6 +3975,10 @@ [email protected]:
39303975
dependencies:
39313976
extsprintf "1.0.2"
39323977

3978+
vlq@^0.2.1:
3979+
version "0.2.2"
3980+
resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.2.tgz#e316d5257b40b86bb43cb8d5fea5d7f54d6b0ca1"
3981+
39333982
well-known-symbols@^1.0.0:
39343983
version "1.0.0"
39353984
resolved "https://registry.yarnpkg.com/well-known-symbols/-/well-known-symbols-1.0.0.tgz#73c78ae81a7726a8fa598e2880801c8b16225518"

0 commit comments

Comments
 (0)