Skip to content

Instantly share code, notes, and snippets.

@cmstead
cmstead / index-example.js
Last active April 5, 2018 19:37
Dject main application example
'use strict';
// The only explicit filesystem or module reference in our whole application!!!
const container = require('./example-container');
const fileReader = container.build('fileReader');
// installed and system modules are loaded implicitly, no extra declaration needed
const path = container.build('path');
const contents = fileReader.readAFile(path.join(__dirname, 'staticFiles', 'myFile.txt'));
@cmstead
cmstead / example-container.js
Last active April 5, 2018 19:37
Dject example configuration
'use strict';
const path = require('path');
const config = {
cwd: path.join(__dirname, 'dependencies'),
modulePaths: [
'file-tools',
'logging-tools'
]
@cmstead
cmstead / file-reader-factory.js
Last active April 5, 2018 19:06
Updated Factory Function Setup
function fileReader(fs, logger) {
function readAFile(filepath) {
try {
return fs.readFileSync(filePath, { encoding: 'utf8' });
} catch (e) {
logger.log(e.message);
return null;
}
}
@cmstead
cmstead / basic-require-example.js
Last active April 5, 2018 19:00
Removing Require and Import
'use strict';
const logger = require('../loggerFactory').create();
const fs = require('fs');
function readAFile(filepath) {
try {
return fs.readFileSync(filePath, { encoding: 'utf8' });
} catch (e) {
logger.log(e.message);
@cmstead
cmstead / no-terminators-example.js
Last active March 20, 2018 19:18
A small example written in Javascript...
const someStuff = []
(function () {
'use strict'
someStuff.push(1)
const logThings = (x => console.log(x))
[2, 3, 4, 5]
@cmstead
cmstead / setCombinator.js
Created March 11, 2018 19:29
A simple, generic set combinator, because reasons.
(function () {
'use strict';
const buildCombination =
(currentList, values) =>
values.map((value) => currentList.concat([value]));
const buildPartialCombination =
(newSet) =>
(setCombination, value) =>
@cmstead
cmstead / windowFakeAndTesting.js
Created February 15, 2018 22:39
Testing when using window
describe('myTest', function () {
it('should interact with window', function () {
function AudioContext() {
this.foo = 'bar';
}
AudioContext.prototype = {
doThatThingYouDo: sinon.spy()
};
@cmstead
cmstead / astHelper.test.js
Created December 29, 2017 00:18
AST Helper Test File
'use strict';
const assert = require('chai').assert;
const sinon = require('sinon');
const container = require('../../../container');
const motherContainer = require('../../test-utils/mother-container');
const testUtils = require('../../test-utils/test-utils');
require('../../test-utils/approvalsConfig');
@cmstead
cmstead / typeHelperExample.js
Created December 28, 2017 23:08
Simplified type helper for JS Refactor example source types
'use strict';
const signet = require('signet')();
(function () {
signet.defineDuckType('astPosition', {
line: 'leftBoundedInt<1>',
column: 'leftBoundedInt<0>'
@cmstead
cmstead / astHelper.js
Created December 28, 2017 23:00
Ast Helper File from JS Refactor source code
'use strict';
function astHelper(estraverse, typeHelper) {
function noOp() { }
function coordsContainedIn(containerCoords, testCoords) {
const onOrAfterStart = testCoords.start.line > containerCoords.start.line
|| (testCoords.start.line === containerCoords.start.line
&& testCoords.start.column >= containerCoords.start.column);