Last active
August 29, 2015 14:16
-
-
Save tracker1/4e789c909300f8cfe025 to your computer and use it in GitHub Desktop.
This is functional JavaScript - composition first
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('cc-globals'); //establishes fetch and R (ramda library) globally | |
//regular expression for valid etcd address | |
var address = /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:\d{1,5}$/; | |
//if the input is a string, makes it an array containing the string, otherwise returns the original input | |
var fixStr = (v) => typeof v === 'string' ? [v] : v; | |
//converts collection input to an array, returns empty array otherwise | |
var fixArray = (v) => v && v.length && Array.prototype.slice.call(v) || []; | |
//forces input to string and trims it | |
var mapFixString = R.map((v) => (v || '').toString().trim()); | |
//function will act as filter against an array, each item must match the address regex | |
var validFilter = R.filter((v) => address.test(v)); | |
//will filter out an empty array, returning null, or an array with items | |
var checkList = (a) => a.length ? a : null; | |
//method wrapping composition of filters | |
var filterEtcdConfig = R.compose(checkList, validFilter, mapFixString, fixArray, fixStr); | |
//expose final composed method | |
module.exports = filterEtcdConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('cc-globals'); | |
//utilities for testing | |
var chai = require('chai') | |
,expect = chai.expect | |
; | |
describe('filter-etcd-config', function(){ | |
var sut = require('../../../src/normalize-options/filter-etcd-config'); | |
it('will return expected valid values', function(){ | |
expect(sut(['127.1.0.1:4001','invalid',null,123])).to.deep.equal(['127.1.0.1:4001']); | |
}); | |
it('will convert a string', function(){ | |
expect(sut('127.1.0.2:4001')).to.deep.equal(['127.1.0.2:4001']); | |
}); | |
it('will return null for invalid or empty results',function(){ | |
return expect(sut(-1)).to.be.null; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment