Skip to content

Commit 345a4b7

Browse files
author
fabianmoronzirfas
committed
modified: bin/concat.js
modified: index.js new file: package-lock.json modified: package.json new file: test/array.from.test.jsx
1 parent 8513023 commit 345a4b7

File tree

5 files changed

+103
-9
lines changed

5 files changed

+103
-9
lines changed

bin/concat.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
const folders = ['./Array/', './Function/', './Object/', './String/'];
2-
// const testFolder = './Array/';
32
const fs = require('fs');
43
const path = require('path');
54
const cat = require('cat');
65
const bundlePath = path.resolve(process.cwd(), './index.js');
7-
86
// clear the file
97
fs.writeFileSync(path.resolve(process.cwd(), bundlePath), '');
108
// loop folders
119
folders.forEach(folder => {
1210
fs.readdir(folder, (err, files) => {
1311
files.forEach(file => {
14-
// console.log(file);
1512
let filePath = path.resolve(process.cwd(), `${folder}${file}`);
16-
// console.log(filePath);
13+
if(file === '.gitkeep') {
14+
return;
15+
}
1716
cat(filePath, (error, data)=>{
1817
if(error !== null) {
1918
console.log(error);
@@ -23,5 +22,4 @@ folders.forEach(folder => {
2322
});
2423
});
2524
});
26-
2725
});

index.js

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,82 @@
1-
//.gitkeep
1+
//from.js
2+
// Production steps of ECMA-262, Edition 6, 22.1.2.1
3+
if (!Array.from) {
4+
Array.from = (function () {
5+
var toStr = Object.prototype.toString;
6+
var isCallable = function (fn) {
7+
return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
8+
};
9+
var toInteger = function (value) {
10+
var number = Number(value);
11+
if (isNaN(number)) { return 0; }
12+
if (number === 0 || !isFinite(number)) { return number; }
13+
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
14+
};
15+
var maxSafeInteger = Math.pow(2, 53) - 1;
16+
var toLength = function (value) {
17+
var len = toInteger(value);
18+
return Math.min(Math.max(len, 0), maxSafeInteger);
19+
};
220

3-
//.gitkeep
21+
// The length property of the from method is 1.
22+
return function from(arrayLike/*, mapFn, thisArg */) {
23+
// 1. Let C be the this value.
24+
var C = this;
425

5-
//.gitkeep
26+
// 2. Let items be ToObject(arrayLike).
27+
var items = Object(arrayLike);
28+
29+
// 3. ReturnIfAbrupt(items).
30+
if (arrayLike == null) {
31+
throw new TypeError('Array.from requires an array-like object - not null or undefined');
32+
}
33+
34+
// 4. If mapfn is undefined, then let mapping be false.
35+
var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
36+
var T;
37+
if (typeof mapFn !== 'undefined') {
38+
// 5. else
39+
// 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
40+
if (!isCallable(mapFn)) {
41+
throw new TypeError('Array.from: when provided, the second argument must be a function');
42+
}
43+
44+
// 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
45+
if (arguments.length > 2) {
46+
T = arguments[2];
47+
}
48+
}
49+
50+
// 10. Let lenValue be Get(items, "length").
51+
// 11. Let len be ToLength(lenValue).
52+
var len = toLength(items.length);
53+
54+
// 13. If IsConstructor(C) is true, then
55+
// 13. a. Let A be the result of calling the [[Construct]] internal method
56+
// of C with an argument list containing the single item len.
57+
// 14. a. Else, Let A be ArrayCreate(len).
58+
var A = isCallable(C) ? Object(new C(len)) : new Array(len);
59+
60+
// 16. Let k be 0.
61+
var k = 0;
62+
// 17. Repeat, while k < len… (also steps a - h)
63+
var kValue;
64+
while (k < len) {
65+
kValue = items[k];
66+
if (mapFn) {
67+
A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
68+
} else {
69+
A[k] = kValue;
70+
}
71+
k += 1;
72+
}
73+
// 18. Let putStatus be Put(A, "length", len, true).
74+
A.length = len;
75+
// 20. Return A.
76+
return A;
77+
};
78+
}());
79+
}
680

781
//assign.js
882
/**

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"bundle":"node bin/concat.js"
8+
"bundle": "node bin/concat.js"
99
},
1010
"repository": {
1111
"type": "git",

test/array.from.test.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "../Array/from.js"
2+
$.writeln(Array.from("foo"));
3+
// => f,o,o
4+
#target "indesign"
5+
var doc = app.documents.add();
6+
doc.pages.add();
7+
$.writeln(Array.from());
8+
// => [object Page], [object Page]

0 commit comments

Comments
 (0)