Skip to content
Open
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
5 changes: 5 additions & 0 deletions lab-david/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
26 changes: 26 additions & 0 deletions lab-david/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"env": {
"browser": true,
"node": true,
"commonjs": true,
"jest": true,
"es6": true
},
"globals": {
"err": true,
"req": true,
"res": true,
"next": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"semi": [ "error", "always" ]
}
}
100 changes: 100 additions & 0 deletions lab-david/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

# Created by https://www.gitignore.io/api/node,macos,visualstudiocode

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history

# End of https://www.gitignore.io/api/node,macos,visualstudiocodes
Empty file added lab-david/README.md
Empty file.
78 changes: 78 additions & 0 deletions lab-david/__test__/fp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

const fp = require('../lib/fp.js');

describe('fp.reduce', () => {
test('applies a function against an accumulator and each element in the array to reduce it to a single value', () => {
expect(fp.reduce(
(accumulator, currentValue) => {
return accumulator + currentValue;
},
[3,3,3],
0
)).toBe(9);
});

test('An exception should be thrown if the callback is not a function', () => {
expect(
() => {
fp.reduce(`I'm totally a function ()`, [3,3,3], 0);
}
).toThrow();
});
});

describe('fp.map', () => {
test('applies a function against each element in the array and creates a new array', () => {
expect(fp.map(
(x) => {
return x + 1;
},
[3,3,3]
)).toEqual([4,4,4]);
});

test('An exception should be thrown if the callback is not a function', () => {
expect(
() => {
fp.map(`I'm totally a function ()`, [3,3,3], 0);
}
).toThrow();
});
});

describe('fp.filter', () => {
test('creates a new array with all elements that pass the test by the function', () => {
expect(fp.filter(
(x) => {
return x > 0;
},
[0, 1, 2]
)).toEqual([1, 2]);
});

test('an exception should be thrown if the callback is not a function', () => {
expect(
() => {
fp.filter(`I'm totally a function ()`, [3,3,3], 0);
}
).toThrow();
});
});

describe('fp.slice', () => {
test('returns a portion of the old array into a new array according to position specified', () => {
expect(fp.slice(
0,
[0,1,2,3]
)).toEqual([0]);
});

test('an exception should be thrown if the collection is not an array-like object', () => {
expect(
() => {
fp.slice(1, 2, 3);
}
).toThrow();
});
});
27 changes: 27 additions & 0 deletions lab-david/lib/fp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const fp = module.exports = {};

fp.reduce = (callback, collection, initialValue) => {
if(typeof callback !== 'function')
throw new TypeError('<callback> should be a function!');
return Array.prototype.reduce.call(collection, callback, initialValue);
};

fp.map = (callback, collection) => {
if(typeof callback !== 'function')
throw new TypeError('<callback> should be a function!');
return Array.prototype.map.call(collection, callback);
};

fp.filter = (callback, collection) => {
if(typeof callback !== 'function')
throw new TypeError('<callback> should be a function!');
return Array.prototype.filter.call(collection, callback);
};

fp.slice = (begin, end, collection) => {
if(typeof callback !== 'object')
throw new TypeError('<collection> should be an array like object');
return Array.prototype.slice.call(collection, begin, end);
};
Loading