Script and tag cleanup, fixed select tests

This commit is contained in:
Angelos Chalaris
2018-01-16 15:15:51 +02:00
parent 336ce6f72b
commit 2a61e7671e
8 changed files with 33 additions and 24 deletions

View File

@ -12,7 +12,8 @@ script:
- npm run linter
- npm run builder
- npm run webber
- npm run module
- npm run packager
- npm run tester
after_success:
- chmod +x .travis/push.sh
- .travis/push.sh

View File

@ -26,8 +26,8 @@
"linter": "node ./scripts/lint.js",
"tagger": "node ./scripts/tag.js",
"webber": "node ./scripts/web.js",
"tdd": "node ./scripts/tdd.js",
"module": "node ./scripts/module.js",
"tester": "node ./scripts/tdd.js",
"packager": "node ./scripts/module.js",
"test": "tape test/**/*.test.js | tap-spec"
},
"repository": {

View File

@ -19,7 +19,7 @@ const IMPORTS = './imports.js';
// Regex for selecting code blocks
const codeRE = /```\s*js([\s\S]*?)```/;
// Start the timer of the script
console.time('Module');
console.time('Packager');
// Load tag data from the database and snippets from their folder
try {
const tagDatabase = fs.readFileSync('tag_database', 'utf8');
@ -72,7 +72,7 @@ try {
// Log a success message
console.log(`${chalk.green('SUCCESS!')} Snippet module built!`);
// Log the time taken
console.timeEnd('Module');
console.timeEnd('Packager');
} catch (err) {
// Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During module creation: ${err}`);

View File

@ -59,7 +59,7 @@ try {
tagDbStats = Object.entries(tagDbData)
.sort((a, b) => a[1][0].localeCompare(b[1][0]))
.reduce((acc, val) => {
acc.hasOwnProperty(val[1]) ? acc[val[1]]++ : (acc[val[1]] = 1);
val[1].forEach(v => acc.hasOwnProperty(v) ? acc[v]++ : (acc[v] = 1));
return acc;
}, {});
} catch (err) {

View File

@ -4,7 +4,8 @@
*/
// Load modules
const fs = require('fs-extra');
const fs = require('fs-extra'), path = require('path');
const child_process = require('child_process');
const chalk = require('chalk');
// Load helper functions (these are from existing snippets in 30 seconds of code!)
const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;
@ -14,7 +15,7 @@ if(isTravisCI() && process.env['TRAVIS_EVENT_TYPE'] !== 'cron' && process.env['T
}
// Declare paths
const SNIPPETS_ACTIVE = './snippets';
const SNIPPETS_ARCHIVE = './snippets_archive';
const SNIPPETS_ARCHIVE = './snippets_archive';
const TEST_PATH = './test';
// Array of snippet names
@ -29,12 +30,12 @@ snippetFiles.push(...snippetFilesArchive);
// Current Snippet that depend on node_modules
const errSnippets = ['JSONToFile', 'readFileLines', 'UUIDGeneratorNode'];
console.time('Tester');
snippetFiles
.filter(fileName => !errSnippets.includes(fileName))
.map(fileName => {
// Check if fileName for snippet exist in test/ dir, if doesnt create
fs.ensureDirSync(`${TEST_PATH}/${fileName}`);
fs.ensureDirSync(path.join(TEST_PATH,fileName));
// return fileName for later use
return fileName;
@ -42,7 +43,7 @@ snippetFiles
.map(fileName => {
const activeOrArchive = snippetFilesActive.includes(fileName) ? SNIPPETS_ACTIVE : SNIPPETS_ARCHIVE;
// Grab snippetData
const fileData = fs.readFileSync(`${activeOrArchive}/${fileName}.md`, 'utf8');
const fileData = fs.readFileSync(path.join(activeOrArchive,`${fileName}.md`), 'utf8');
// Grab snippet Code blocks
const fileCode = fileData.slice(fileData.search(/```\s*js/i), fileData.lastIndexOf('```') + 3);
// Split code based on code markers
@ -55,7 +56,7 @@ snippetFiles
.split('\n')
.map(line => line.trim())
.filter((_, i) => blockMarkers[0] < i && i < blockMarkers[1]);
// Grab snippet example based on code markers
// Grab snippet example based on code markers
const fileExample = fileCode
.split('\n')
.map(line => line.trim())
@ -64,13 +65,13 @@ snippetFiles
// Export template for snippetName.js which takes into account snippet name.length when generating snippetName.js file
const exportFile = `module.exports = ${fileName} = ${fileFunction.join('\n').slice(9 + fileName.length)}`;
// Export template for snippetName.test.js which generates a example test & other information
// Export template for snippetName.test.js which generates a example test & other information
const exportTest = [
`const test = require('tape');`,
`const ${fileName} = require('./${fileName}.js');`,
`\ntest('Testing ${fileName}', (t) => {`,
`\t//For more information on all the methods supported by tape\n\t//Please go to https://github.com/substack/tape`,
`\tt.true(typeof ${fileName} === 'function', '${fileName} is a Function');`,
`\tt.true(typeof ${fileName} === 'function', '${fileName} is a Function');`,
`\t//t.deepEqual(${fileName}(args..), 'Expected');`,
`\t//t.equal(${fileName}(args..), 'Expected');`,
`\t//t.false(${fileName}(args..), 'Expected');`,
@ -80,14 +81,21 @@ snippetFiles
].join('\n');
// Write/Update exportFile which is snippetName.js in respective dir
fs.writeFileSync(`${TEST_PATH}/${fileName}/${fileName}.js`, exportFile);
fs.writeFileSync(path.join(TEST_PATH,fileName,`${fileName}.js`), exportFile);
if ( !fs.existsSync(`${TEST_PATH}/${fileName}/${fileName}.test.js`) ) {
// if snippetName.test.js doesn't exist inrespective dir exportTest
if ( !fs.existsSync(path.join(TEST_PATH,fileName,`${fileName}.test.js`)) ) {
// if snippetName.test.js doesn't exist inrespective dir exportTest
fs.writeFileSync(`${TEST_PATH}/${fileName}/${fileName}.test.js`, exportTest);
}
// return fileName for later use
return fileName;
});
try {
fs.writeFileSync(path.join(TEST_PATH,'testlog'),`Test log for: ${new Date().toString()}\n`);
child_process.execSync(`npm test >> ${TEST_PATH}/testlog`);
}
catch (e) {
fs.appendFileSync(path.join(TEST_PATH,'testlog'));
}
console.timeEnd('Tester');

View File

@ -75,7 +75,7 @@ indexOfAll:array
initial:array
initialize2DArray:array
initializeArrayWithRange:array,math
initializeArrayWithValues:array,amth
initializeArrayWithValues:array,math
inRange:math
intersection:array,math
invertKeyValues:object
@ -116,7 +116,7 @@ median:math,array
memoize:function
merge:object,array
minBy:math,array,function
minN:array,amth
minN:array,math
negate:function
nthElement:array
objectFromPairs:object,array

View File

@ -1,2 +1,2 @@
module.exports = select = (from, selector) =>
selector.split('.').reduce((prev, cur) => prev && prev[cur], from);
module.exports = select = (from, ...selectors) =>
[...selectors].map(s => s.split('.').reduce((prev, cur) => prev && prev[cur], from));

View File

@ -6,10 +6,10 @@ test('Testing select', (t) => {
//Please go to https://github.com/substack/tape
t.true(typeof select === 'function', 'select is a Function');
const obj = { selector: { to: { val: 'val to select' } } };
t.equal(select(obj, 'selector.to.val'), 'val to select', "Retrieve a property indicated by the selector from an object.");
t.deepEqual(select(obj, 'selector.to.val'), ['val to select'], "Retrieve a property indicated by the selector from an object.");
//t.deepEqual(select(args..), 'Expected');
//t.equal(select(args..), 'Expected');
//t.false(select(args..), 'Expected');
//t.throws(select(args..), 'Expected');
t.end();
});
});