Skip to content

Instantly share code, notes, and snippets.

@savayer
Last active October 17, 2019 08:10
Show Gist options
  • Save savayer/1af8a64387f360d2bd2668a653e239ea to your computer and use it in GitHub Desktop.
Save savayer/1af8a64387f360d2bd2668a653e239ea to your computer and use it in GitHub Desktop.
/*
http://dkab.github.io/jasmine-tests/
*/
/*
First test
*/
function sequence(start, step) {
if (!start) {
start = 0;
}
if (!step) {
step = 1;
}
var res = start;
var tmp = start;
return function () {
tmp = res;
res += step;
return tmp;
};
}
/*
Second test
*/
function take(fn, count) {
var results = [];
for (var i = 0; i < count; ++i) {
results.push(fn());
}
return results;
}
/*
Third test
*/
function map(fn, array) {
var results = [];
for (var i = 0; i < array.length; i++) {
results.push(fn(array[i]));
}
return results;
}
/*
Fourth test
*/
function fmap(mixin, fn) {
return function () {
return mixin(fn.apply(this, arguments));
};
}
/*
Fiveth test
*/
function partial() {
var firstFn = arguments[0];
var firstArgs = Array.prototype.slice.call(arguments, 1);
return function () {
var args = Array.prototype.slice.call(arguments);
return firstFn.apply(null, firstArgs.concat(args));
};
}
/*
Sixth test
*/
function partialAny() {
var firstFn = arguments[0];
var firstArgs = Array.prototype.slice.call(arguments, 1);
return function () {
var firstArgsTmp = Array.prototype.slice.call(firstArgs);
var args = Array.prototype.slice.call(arguments);
var j = 0;
for (var i = 0; i < firstArgs.length; ++i) {
if (firstArgsTmp[i] === undefined) {
firstArgsTmp[i] = args[j];
++j;
}
}
if (j < args.length) {
for (var i = j; i < firstArgs.length + 1; ++i) {
firstArgsTmp.push(args[i]);
}
}
return firstFn.apply(null, firstArgsTmp);
};
}
/*
Seventh test
*/
function bind (self, context) {
return function () {
return self.bind(context).apply(null, arguments);
};
};
/*
Eight test
*/
function pluck (array, key) {
var results = [];
for (var i = 0; i < array.length; ++i) {
var object = array[i];
results.push(object[key]);
}
return results;
};
/*
Nineth test
*/
function filter (array, fn) {
var results = [];
for (var i = 0; i < array.length; ++i) {
if (fn(array[i])) {
results.push(array[i]);
}
}
return results;
};
/*
Tenth test
*/
function count (obj) {
return Object.keys(obj).length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment