Skip to content

Instantly share code, notes, and snippets.

@mrenrich84
mrenrich84 / inject_behaviour.js
Last active Aug 14, 2017
Jest: mocking externally dependencies
View inject_behaviour.js
// TestSubject.js
const ExternalDependency = require('./ExternalDependency')
module.exports = class TestSubject {
constructor () {
this.externalDependency = new ExternalDependency()
}
isExternalDependencyMocked () {
return this.externalDependency.isMocked
@mrenrich84
mrenrich84 / Error.js
Created Aug 12, 2017
CustomError in NodeJS
View Error.js
module.exports = function CustomError(message) {
this.name = this.constructor.name
this.message = message
Error.captureStackTrace(this, this.constructor)
}
module.exports.prototype.inspect = function () {
return this.stack
}
View extendingSubstackPattern.js
//file substack.js
module.exports = function() {
console.log('main entry point');
};
module.exports.details = function() {
console.log('more detailed functionality');
};
View substackPattern.js
function getDatasetfromJsonData(jsonData){
return jsonData[0].scores;
};
function getLabels(dataset) {
return Object.keys(dataset)
};
function getData(dataset) {
return Object.keys(dataset).map(function(key) {
View callingTheModule.js
// ...IN THE CALLER
import JSONDataConverter from './JSONDataConverter';
<RadarChart data={JSONDataConverter(data)} width={width} height={height} redraw />
View module_pattern.js
var JSONDataConverter = (function () {
var getDatasetfromJsonData = function(jsonData){
return jsonData[0].scores;
};
var getLabels = function(dataset) {
return Object.keys(dataset)
};
var getData = function(dataset) {
View DataConverter.js
class DataConverterContainer extends Component {
getDatasetfromJsonData(jsonData){
return jsonData[0].scores;
};
getLabels(dataset){
return Object.keys(dataset)
};
getData(dataset){
@mrenrich84
mrenrich84 / avgNull.js
Created Sep 9, 2015
Javascript: average which accept null values
View avgNull.js
16 lines (14 sloc) 0.36 kB
function average(data) {
/*Can't find an average function in JS, made one
This function is able to handle null values!!!*/
var count = null;
var sum = null;
for (i=0; i<data.length; i++) {
if (data[i]) {
count++;