simple-protocol-helpers
Advanced tools
Comparing version 0.3.0 to 1.0.1
@@ -16,2 +16,4 @@ 'use strict'; | ||
var assign = require('lodash/assign'); | ||
var some = require('lodash/some'); | ||
var find = require('lodash/find'); | ||
@@ -101,2 +103,10 @@ var isProtocol = function isProtocol(s) { | ||
var hasAnyFailures = function hasAnyFailures(l) { | ||
return some(l, isFailure); | ||
}; | ||
var getFirstFailure = function getFirstFailure(l) { | ||
return find(l, isFailure); | ||
}; | ||
module.exports = { | ||
@@ -115,4 +125,6 @@ getSuccesses: getSuccesses, | ||
errorToObject: errorToObject, | ||
clean: clean | ||
clean: clean, | ||
hasAnyFailures: hasAnyFailures, | ||
getFirstFailure: getFirstFailure | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -18,3 +18,5 @@ 'use strict'; | ||
isProtocol = _require2.isProtocol, | ||
clean = _require2.clean; | ||
clean = _require2.clean, | ||
hasAnyFailures = _require2.hasAnyFailures, | ||
getFirstFailure = _require2.getFirstFailure; | ||
@@ -331,3 +333,35 @@ describe('index.js', function () { | ||
}); | ||
describe('hasAnyFailures()', function () { | ||
it('should return true if there are any failures', function () { | ||
var l = [success(), failure()]; | ||
var actual = hasAnyFailures(l); | ||
var expected = true; | ||
deepEqual(actual, expected); | ||
}); | ||
it('should return false if there are no failures', function () { | ||
var l = [success(), success()]; | ||
var actual = hasAnyFailures(l); | ||
var expected = false; | ||
deepEqual(actual, expected); | ||
}); | ||
}); | ||
describe('getFirstFailure()', function () { | ||
it('should return first failure', function () { | ||
var l = [success(), failure('foo'), failure('bar')]; | ||
var actual = getFirstFailure(l); | ||
var expected = failure('foo'); | ||
deepEqual(actual, expected); | ||
}); | ||
it('should return falsey value if no failures', function () { | ||
var l = [success(), success()]; | ||
var actual = getFirstFailure(l); | ||
var expected = undefined; | ||
deepEqual(actual, expected); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.spec.js.map |
{ | ||
"name": "simple-protocol-helpers", | ||
"version": "0.3.0", | ||
"version": "1.0.1", | ||
"description": "A small library for using simple protocol", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,1 +0,23 @@ | ||
# Simple Protocol | ||
## What is simple protocol? | ||
Simple protocol is *simple*: | ||
1) Never intentionally throw exceptions. | ||
2) Return a valid JSON object like this for a success: | ||
``` | ||
{ | ||
success: true, | ||
payload: { | ||
// the result of the operation, i.e. an http response body | ||
} | ||
} | ||
``` | ||
3) Return a valid JSON object like this for an error: | ||
``` | ||
{ | ||
success: false, | ||
error: { | ||
// error details or object | ||
} | ||
} | ||
``` | ||
That's it! Both success and error cases are handled the same way and can follow the same code path. |
@@ -6,4 +6,6 @@ const isSuccess = p => !!p && p.success === true | ||
const assign = require('lodash/assign') | ||
const some = require('lodash/some') | ||
const find = require('lodash/find') | ||
const isProtocol = (s) => { | ||
const isProtocol = s => { | ||
if (!s) return false | ||
@@ -14,9 +16,9 @@ if (hasSuccess(s) && (hasPayload(s) || hasError(s))) return true | ||
const normalize = (fn, p) => isProtocol(p) ? p : fn(p) | ||
const normalize = (fn, p) => (isProtocol(p) ? p : fn(p)) | ||
const normalizeToSuccess = p => normalize(success, p) | ||
const normalizeToFailure = p => normalize(failure, p) | ||
const normalizeListToSuccess = (l) => l.map(normalizeToSuccess) | ||
const normalizeListToFailure = (l) => l.map(normalizeToFailure) | ||
const normalizeListToSuccess = l => l.map(normalizeToSuccess) | ||
const normalizeListToFailure = l => l.map(normalizeToFailure) | ||
const errorToObject = (e) => { | ||
const errorToObject = e => { | ||
const props = Object.getOwnPropertyNames(e).concat('name') | ||
@@ -31,6 +33,9 @@ return props.reduce((p, c) => { | ||
if (isSuccess(payload)) return assign(payload, props) | ||
return assign({ | ||
success: true, | ||
payload | ||
}, props) | ||
return assign( | ||
{ | ||
success: true, | ||
payload | ||
}, | ||
props | ||
) | ||
} | ||
@@ -47,13 +52,16 @@ | ||
return assign({ | ||
success: false, | ||
error | ||
}, props) | ||
return assign( | ||
{ | ||
success: false, | ||
error | ||
}, | ||
props | ||
) | ||
} | ||
const hasError = (s) => s.hasOwnProperty('error') | ||
const hasPayload = (s) => s.hasOwnProperty('payload') | ||
const hasSuccess = (s) => s.hasOwnProperty('success') | ||
const hasError = s => s.hasOwnProperty('error') | ||
const hasPayload = s => s.hasOwnProperty('payload') | ||
const hasSuccess = s => s.hasOwnProperty('success') | ||
const clean = (s) => { | ||
const clean = s => { | ||
if (hasPayload(s)) { | ||
@@ -72,2 +80,10 @@ return { | ||
const hasAnyFailures = l => { | ||
return some(l, isFailure) | ||
} | ||
const getFirstFailure = l => { | ||
return find(l, isFailure) | ||
} | ||
module.exports = { | ||
@@ -86,3 +102,5 @@ getSuccesses, | ||
errorToObject, | ||
clean | ||
clean, | ||
hasAnyFailures, | ||
getFirstFailure | ||
} |
@@ -14,3 +14,5 @@ const { deepEqual } = require('assert') | ||
isProtocol, | ||
clean | ||
clean, | ||
hasAnyFailures, | ||
getFirstFailure | ||
} = require('./index') | ||
@@ -328,2 +330,34 @@ | ||
}) | ||
describe('hasAnyFailures()', () => { | ||
it('should return true if there are any failures', () => { | ||
const l = [success(), failure()] | ||
const actual = hasAnyFailures(l) | ||
const expected = true | ||
deepEqual(actual, expected) | ||
}) | ||
it('should return false if there are no failures', () => { | ||
const l = [success(), success()] | ||
const actual = hasAnyFailures(l) | ||
const expected = false | ||
deepEqual(actual, expected) | ||
}) | ||
}) | ||
describe('getFirstFailure()', () => { | ||
it('should return first failure', () => { | ||
const l = [success(), failure('foo'), failure('bar')] | ||
const actual = getFirstFailure(l) | ||
const expected = failure('foo') | ||
deepEqual(actual, expected) | ||
}) | ||
it('should return falsey value if no failures', () => { | ||
const l = [success(), success()] | ||
const actual = getFirstFailure(l) | ||
const expected = undefined | ||
deepEqual(actual, expected) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
51604
848
1
24