Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased]
### Added
- Reimplement `String.split_at/2` to make sure Unicode library isn't compiled

## [0.30.0] - 2017-08-15

### Added
Expand Down
6 changes: 6 additions & 0 deletions lib/elixir_script/lib/functions.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule ElixirScript.Core.Functions do
@moduledoc false
use ElixirScript.FFI, global: true

defexternal split_at(value, position)
end
4 changes: 4 additions & 0 deletions lib/elixir_script/lib/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,8 @@ defmodule ElixirScript.String do
def valid?(str) do
is_binary(str)
end

def split_at(value, position) do
ElixirScript.Core.Functions.split_at(value, position)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ElixirScript.Mixfile do
def project do
[
app: :elixir_script,
version: "0.30.0",
version: "0.31.0-dev",
elixir: "~> 1.5",
elixirc_paths: elixirc_paths(Mix.env),
deps: deps(),
Expand Down
4 changes: 2 additions & 2 deletions priv/build/iife/ElixirScript.Core.js

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion src/javascript/lib/core/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,40 @@ function trampoline(f) {
return currentValue;
}

function split_at(value, position) {
if (position < 0) {
const newPosition = value.length + position;
if (newPosition < 0) {
return new Core.Tuple('', value);
}

return split_at(value, newPosition);
}

let first = '';
let second = '';
let index = 0;

for (const character of value) {
if (index < position) {
first = first + character;
} else {
second = second + character;
}

index = index + 1;
}

return new Core.Tuple(first, second);
}

export default {
call_property,
defprotocol,
defimpl,
build_namespace,
map_to_object,
trampoline,
Recurse
Recurse,
split_at
};
12 changes: 12 additions & 0 deletions src/javascript/tests/core/functions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ test('call_property', t => {
t.is(Functions.call_property({ completed: false }, 'completed'), false);
t.is(Functions.call_property({ id: 0 }, 'id'), 0);
});

test('split_at', t => {
t.deepEqual(Functions.split_at('sweetelixir', 5).values, ['sweet', 'elixir']);
t.deepEqual(Functions.split_at('sweetelixir', -6).values, [
'sweet',
'elixir'
]);
t.deepEqual(Functions.split_at('abc', 0).values, ['', 'abc']);
t.deepEqual(Functions.split_at('abc', 1000).values, ['abc', '']);
t.deepEqual(Functions.split_at('abc', -1000).values, ['', 'abc']);
t.deepEqual(Functions.split_at('😀abélkm', 4).values, ['😀abé', 'lkm']);
});