fable 3.0.4 → 3.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/source/Fable-Utility.js
CHANGED
|
@@ -24,6 +24,30 @@ class FableUtility
|
|
|
24
24
|
return tmpTemplate.buildTemplateFunction(pTemplateText, pData);
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
// This is a safe, modern version of chunk from underscore
|
|
28
|
+
// Algorithm pulled from a mix of these two polyfills:
|
|
29
|
+
// https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_chunk
|
|
30
|
+
// https://youmightnotneed.com/lodash
|
|
31
|
+
// This implementation was most tolerant in browsers. Uglify can fix the rest.
|
|
32
|
+
chunk (pInput, pChunkSize, pChunkCache)
|
|
33
|
+
{
|
|
34
|
+
let tmpInputArray = [...pInput];
|
|
35
|
+
// Note lodash defaults to 1, underscore defaults to 0
|
|
36
|
+
let tmpChunkSize = (typeof(pChunkSize) == 'number') ? pChunkSize : 0;
|
|
37
|
+
let tmpChunkCache = (typeof(pChunkCache) != 'undefined') ? pChunkCache : [];
|
|
38
|
+
|
|
39
|
+
if (tmpChunkSize <= 0)
|
|
40
|
+
{
|
|
41
|
+
return tmpChunkCache;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
while (tmpInputArray.length)
|
|
45
|
+
{
|
|
46
|
+
tmpChunkCache.push(tmpInputArray.splice(0, tmpChunkSize));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return tmpChunkCache;
|
|
50
|
+
}
|
|
27
51
|
}
|
|
28
52
|
|
|
29
53
|
module.exports = FableUtility;
|
|
@@ -109,6 +109,35 @@ suite
|
|
|
109
109
|
Expect(tmpResult.Size).to.equal(15);
|
|
110
110
|
}
|
|
111
111
|
);
|
|
112
|
+
test
|
|
113
|
+
(
|
|
114
|
+
'chunk should work like underscore',
|
|
115
|
+
function()
|
|
116
|
+
{
|
|
117
|
+
testFable = new libFable();
|
|
118
|
+
// These are *literally* the tests from underscore
|
|
119
|
+
/* Here for posterity
|
|
120
|
+
*
|
|
121
|
+
|
|
122
|
+
*
|
|
123
|
+
*/
|
|
124
|
+
// Regular Expressions for easy conversion of underscore tests:
|
|
125
|
+
// S: assert.deepEqual\(_.chunk\((.*)\), (.*), '
|
|
126
|
+
// R: Expect(testFable.Utility.chunk($1)).to.deep.equal($2); // $3
|
|
127
|
+
Expect(testFable.Utility.chunk([], 2)).to.deep.equal([]); // chunk for empty array returns an empty array');
|
|
128
|
+
|
|
129
|
+
Expect(testFable.Utility.chunk([1, 2, 3], 0)).to.deep.equal([]); // chunk into parts of 0 elements returns empty array');
|
|
130
|
+
Expect(testFable.Utility.chunk([1, 2, 3], -1)).to.deep.equal([]); // chunk into parts of negative amount of elements returns an empty array');
|
|
131
|
+
Expect(testFable.Utility.chunk([1, 2, 3])).to.deep.equal([]); // defaults to empty array (chunk size 0)');
|
|
132
|
+
|
|
133
|
+
Expect(testFable.Utility.chunk([1, 2, 3], 1)).to.deep.equal([[1], [2], [3]]); // chunk into parts of 1 elements returns original array');
|
|
134
|
+
|
|
135
|
+
Expect(testFable.Utility.chunk([1, 2, 3], 3)).to.deep.equal([[1, 2, 3]]); // chunk into parts of current array length elements returns the original array');
|
|
136
|
+
Expect(testFable.Utility.chunk([1, 2, 3], 5)).to.deep.equal([[1, 2, 3]]); // chunk into parts of more then current array length elements returns the original array');
|
|
137
|
+
|
|
138
|
+
Expect(testFable.Utility.chunk([10, 20, 30, 40, 50, 60, 70], 2)).to.deep.equal([[10, 20], [30, 40], [50, 60], [70]]); // chunk into parts of less then current array length elements');
|
|
139
|
+
Expect(testFable.Utility.chunk([10, 20, 30, 40, 50, 60, 70], 3)).to.deep.equal([[10, 20, 30], [40, 50, 60], [70]]); // chunk into parts of less then current array length elements');
|
|
140
|
+
});
|
|
112
141
|
}
|
|
113
142
|
);
|
|
114
143
|
}
|