Skip to content

Commit

Permalink
stdlib [array]: Better array joining
Browse files Browse the repository at this point in the history
  • Loading branch information
Vihan committed Jul 10, 2016
1 parent 52ca626 commit 6627f70
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/stdlib/primitive/Array/lib/collapse.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default (api) => ["collapse", api.var(new api.func(
[
["dimensions", {
Type: api.string,
Splat: true
}]
],
function(scope, input) {
// TODO:
}
))];
28 changes: 28 additions & 0 deletions src/stdlib/primitive/Array/lib/fuse.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default (api) => ["fuse", api.prop(new api.func(
[],
function(scope, input) {
let self = input("self").value;

let stringified = "",
cast, text;

for (let i = 0; i < self.length; i++) {
cast = (self[i] instanceof api.string) || self[i].constructor.Cast.get('String');

if (cast) {
if (cast === true) {
text = self[i].value;
} else {
text = cast(self[i]).value;
}
stringified += "" + text;
} else {
return `Cannot stringify \`${
self[i].constructor.Name || self[i].Name
}\` in join`;
}
}

return api.init(api.string, stringified);
}
))]
18 changes: 18 additions & 0 deletions test/tests/primitives/op/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var TestCheddarFrom = require('../../globals').TestCheddarFrom;
var chai = require('chai');
var expect = chai.expect;
chai.should();

describe('Array', function() {
describe('has', function() {
it('should print true', TestCheddarFrom.Code(
'print [1] has 1',
'true'
))

it('should print false', TestCheddarFrom.Code(
'print [1] has 2',
'false'
))
})
});

0 comments on commit 6627f70

Please sign in to comment.