Merge branch 'master' into lev-dist

This commit is contained in:
Angelos Chalaris
2018-02-26 17:32:47 +02:00
committed by GitHub
35 changed files with 739 additions and 269 deletions

View File

@ -1,11 +0,0 @@
const anagrams = str => {
if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
return str
.split('')
.reduce(
(acc, letter, i) =>
acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)),
[]
);
};
module.exports = anagrams;

View File

@ -1,16 +0,0 @@
const test = require('tape');
const anagrams = require('./anagrams.js');
test('Testing anagrams', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof anagrams === 'function', 'anagrams is a Function');
t.deepEqual(anagrams('abc'), ['abc','acb','bac','bca','cab','cba'], "Generates all anagrams of a string");
t.deepEqual(anagrams('a'), ['a'], "Works for single-letter strings");
t.deepEqual(anagrams(''), [''], "Works for empty strings");
//t.deepEqual(anagrams(args..), 'Expected');
//t.equal(anagrams(args..), 'Expected');
//t.false(anagrams(args..), 'Expected');
//t.throws(anagrams(args..), 'Expected');
t.end();
});

View File

@ -0,0 +1,11 @@
const isAnagram = (str1, str2) => {
const normalize = str =>
str
.toLowerCase()
.replace(/[^a-z0-9]/gi, '')
.split('')
.sort()
.join('');
return normalize(str1) === normalize(str2);
};
module.exports = isAnagram;

View File

@ -0,0 +1,17 @@
const test = require('tape');
const isAnagram = require('./isAnagram.js');
test('Testing isAnagram', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof isAnagram === 'function', 'isAnagram is a Function');
t.true(isAnagram('iceman', 'cinema'), 'Checks valid anagram');
t.true(isAnagram('rail safety', 'fairy tales'), 'Works with spaces');
t.true(isAnagram('roast beef', 'eat for BSE'), 'Ignores case');
t.true(isAnagram('Regera Dowdy', 'E. G. Deadworry'), 'Ignores special characters');
//t.deepEqual(isAnagram(args..), 'Expected');
//t.equal(isAnagram(args..), 'Expected');
//t.false(isAnagram(args..), 'Expected');
//t.throws(isAnagram(args..), 'Expected');
t.end();
});

5
test/nest/nest.js Normal file
View File

@ -0,0 +1,5 @@
const nest = (items, id = null, link = 'parent_id') =>
items
.filter(item => item[link] === id)
.map(item => ({ ...item, children: nest(items, item.id) }));
module.exports = nest;

13
test/nest/nest.test.js Normal file
View File

@ -0,0 +1,13 @@
const test = require('tape');
const nest = require('./nest.js');
test('Testing nest', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof nest === 'function', 'nest is a Function');
//t.deepEqual(nest(args..), 'Expected');
//t.equal(nest(args..), 'Expected');
//t.false(nest(args..), 'Expected');
//t.throws(nest(args..), 'Expected');
t.end();
});

3
test/pad/pad.js Normal file
View File

@ -0,0 +1,3 @@
const pad = (string, length = 8, char = ' ') =>
string.padStart((string.length + length) / 2, char).padEnd(length, char);
module.exports = pad;

13
test/pad/pad.test.js Normal file
View File

@ -0,0 +1,13 @@
const test = require('tape');
const pad = require('./pad.js');
test('Testing pad', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof pad === 'function', 'pad is a Function');
t.equal(pad('cat'), ' cat ', 'cat is padded on both sides');
t.equal(pad('cat').length, 8, 'length of string is 8');
t.equal(pad(String(42), 6, '0'), '004200', 'pads 42 with "0"');
t.equal(pad('foobar', 3), 'foobar', 'does not truncates if string exceeds length');
t.end();
});

View File

@ -0,0 +1,11 @@
const permutations = arr => {
if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;
return arr.reduce(
(acc, item, i) =>
acc.concat(
permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [item, ...val])
),
[]
);
};
module.exports = permutations;

View File

@ -0,0 +1,14 @@
const test = require('tape');
const permutations = require('./permutations.js');
test('Testing permutations', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof permutations === 'function', 'permutations is a Function');
t.deepEqual(permutations([1, 33, 5]), [ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5, 1 ], [ 5, 1, 33 ], [ 5, 33, 1 ] ], 'Generates all permutations of an array');
//t.deepEqual(permuteAll(args..), 'Expected');
//t.equal(permuteAll(args..), 'Expected');
//t.false(permuteAll(args..), 'Expected');
//t.throws(permuteAll(args..), 'Expected');
t.end();
});

View File

@ -0,0 +1,11 @@
const stringPermutations = str => {
if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
return str
.split('')
.reduce(
(acc, letter, i) =>
acc.concat(stringPermutations(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)),
[]
);
};
module.exports = stringPermutations;

View File

@ -0,0 +1,16 @@
const test = require('tape');
const stringPermutations = require('./stringPermutations.js');
test('Testing stringPermutations', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof stringPermutations === 'function', 'stringPermutations is a Function');
t.deepEqual(stringPermutations('abc'), ['abc','acb','bac','bca','cab','cba'], "Generates all stringPermutations of a string");
t.deepEqual(stringPermutations('a'), ['a'], "Works for single-letter strings");
t.deepEqual(stringPermutations(''), [''], "Works for empty strings");
//t.deepEqual(anagrams(args..), 'Expected');
//t.equal(anagrams(args..), 'Expected');
//t.false(anagrams(args..), 'Expected');
//t.throws(anagrams(args..), 'Expected');
t.end();
});

View File

@ -1,8 +1,9 @@
const tomorrow = () => {
const tomorrow = (long = false) => {
let t = new Date();
t.setDate(t.getDate() + 1);
return `${t.getFullYear()}-${String(t.getMonth() + 1).padStart(2, '0')}-${String(
const ret = `${t.getFullYear()}-${String(t.getMonth() + 1).padStart(2, '0')}-${String(
t.getDate()
).padStart(2, '0')}`;
return !long ? ret : `${ret}T00:00:00`;
};
module.exports = tomorrow;