@ebay/retriever
Advanced tools
Comparing version 1.0.0 to 1.1.0
10
index.js
@@ -87,3 +87,3 @@ 'use strict'; | ||
if (logger && eventType) { | ||
if (logger && logType && eventType) { | ||
log(eventType, path, defaultValue, logType); | ||
@@ -100,3 +100,3 @@ } | ||
function get(object, path, defaultValue) { | ||
return access(object, path, defaultValue, 'debug'); | ||
return access(object, path, defaultValue); | ||
} | ||
@@ -115,6 +115,2 @@ | ||
if (logger && !result) { | ||
log(EVENT_TYPES.DATA_MISSING, path, false, 'debug'); | ||
} | ||
return result; | ||
@@ -125,3 +121,3 @@ } | ||
* Set logger to be used for all future usage | ||
* @param object l - the logger with debug and warn functions | ||
* @param object l - the logger with a warn function | ||
*/ | ||
@@ -128,0 +124,0 @@ function setLogger(l) { |
{ | ||
"name": "@ebay/retriever", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "retrieve nested data safely", | ||
@@ -9,4 +9,6 @@ "main": "index.js", | ||
"test": "istanbul cover _mocha test/index.js && node test/perf.js", | ||
"build": "yarn run lint && yarn test", | ||
"rep": "open coverage/lcov-report/index.html", | ||
"all": "npm run lint && npm test && npm run rep" | ||
"all": "yarn run lint && yarn test && yarn run rep", | ||
"wrap": "rm -rf node_modules; rm yarn.lock; yarn cache clean && yarn" | ||
}, | ||
@@ -13,0 +15,0 @@ "repository": { |
# retriever | ||
`retriever` is a small utility library for retrieving nested data safely. It contains several improvements over solutions like `lodash.get`, such as convenient defaults, type checking, and optional logging. | ||
`retriever` is a small utility library for retrieving nested data safely. It contains several improvements over solutions like `lodash.get`, such as: | ||
- **Type checking**: Ensure that accessed data is of the exact type that is needed. | ||
- **Optional logging**: Log warnings in cases of required data that is missing or of the wrong type. | ||
- **Convenient defaults**: Retrieving data looks for strings by default, and also provides extra utility for objects. | ||
@@ -18,4 +21,3 @@ ## Installation | ||
r.setLogger({ | ||
debug: function (messageFormat, lookupPath, defaultValue, logType) {}, // used with get() and has() | ||
warn: function (messageFormat, lookupPath, defaultValue, logType) {} // used with need() | ||
warn: function (messageFormat, eventType, lookupPath, defaultValue) {} // used with need() | ||
}); | ||
@@ -55,3 +57,3 @@ | ||
var count = r.need(input, 'model.count', '50'); // '50' (from defaultValue), logs `warning` | ||
var count = r.get(input, 'model.count', '50'); // '50' (from defaultValue), logs `debug` | ||
var count = r.get(input, 'model.count', '50'); // '50' (from defaultValue), does not log | ||
@@ -81,3 +83,3 @@ // defaults to defaultValue when data is missing or of mismatched type | ||
`need()` assumes that the data of the specified type needs to be present. Otherwise, it will log a warning. | ||
`get()` is more lenient, and will only log `debug` instead of `warn`. | ||
`get()` is more lenient, and will not log a warning. | ||
@@ -97,3 +99,2 @@ **Arguments** | ||
Checks if path is a direct property of object, and has a value that is not null or undefined. | ||
This will log `debug` if the data is missing. | ||
@@ -111,9 +112,15 @@ **Arguments** | ||
Sets the logger to be used for logging any issues in retrieving the data. If logging is desired, this should be called once at the start of the app to be used for all subsequent usage. If `retriever` logging is desired on the client, `setLogger` must be initialized in the browser as well. | ||
Sets the logger to be used for logging any issues in retrieving the data. If logging is desired, this should be called once at the start of the app to be used for all subsequent usage. If `retriever` logging is desired on the client, `setLogger` must be initialized in the browser as well. If you are using this with other logging libraries, you'll need to ensure that the logging is enabled per those environment settings. | ||
**Arguments** | ||
- `logger` (Object): A logger object containing the functions `debug` and `warn`. These functions will be called with the following parameters: | ||
`messageFormat`, `lookupPath`, `defaultValue`, `logType`. | ||
- `logger` (Object): A logger object containing the function `warn`. This function will be called with the following parameters: | ||
`messageFormat`, `eventType`, `lookupPath`, `defaultValue`. | ||
For example, a type mismatch warning, the parameters might look like this: | ||
- `messageFormat`: `'event: %s, path: %s, default: %s'` | ||
- `eventType`: `'typeMismatch'` | ||
- `lookupPath`: `'data.path[0]'` | ||
- `default`: `''` | ||
## Similar Projects | ||
@@ -120,0 +127,0 @@ - [Lodash get()](https://lodash.com/docs/#get) |
@@ -115,29 +115,24 @@ 'use strict'; | ||
describe('logging', function () { | ||
['need', 'get', 'has'].forEach(function (accessor) { | ||
[ | ||
{lookup: 'missingKey', event: EVENT_TYPES.DATA_MISSING}, | ||
{lookup: 'a', event: EVENT_TYPES.TYPE_MISMATCH} | ||
].forEach(function (scenario) { | ||
// skip typeMismatch scenario for has() | ||
if (!(accessor === 'has' && scenario.event === EVENT_TYPES.TYPE_MISMATCH)) { | ||
it('logs ' + scenario.event + ' with ' + accessor + '() using supplied logger', function (done) { | ||
var log = function (message, eventType) { | ||
expect(eventType).to.equal(scenario.event); | ||
done(); | ||
}; | ||
var spy = chai.spy(log); | ||
var logType = (accessor === 'need') ? 'warn' : 'debug'; | ||
var mockLogger = {}; | ||
mockLogger[logType] = spy; | ||
[ | ||
{lookup: 'missingKey', event: EVENT_TYPES.DATA_MISSING}, | ||
{lookup: 'a', event: EVENT_TYPES.TYPE_MISMATCH} | ||
].forEach(function (scenario) { | ||
it('logs ' + scenario.event + ' with need() using supplied logger', function (done) { | ||
var log = function (message, eventType) { | ||
expect(eventType).to.equal(scenario.event); | ||
done(); | ||
}; | ||
var spy = chai.spy(log); | ||
var logType = 'warn'; | ||
var mockLogger = {}; | ||
mockLogger[logType] = spy; | ||
r.setLogger(); | ||
r[accessor](basicObject, scenario.lookup); | ||
expect(spy).not.to.have.been.called(); | ||
r.setLogger(); | ||
r.need(basicObject, scenario.lookup); | ||
expect(spy).not.to.have.been.called(); | ||
r.setLogger(mockLogger); | ||
r[accessor](basicObject, scenario.lookup); | ||
}); | ||
} | ||
r.setLogger(mockLogger); | ||
r.need(basicObject, scenario.lookup); | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,3 @@ | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
@@ -6,3 +8,2 @@ | ||
r.setLogger({ | ||
debug: function () {}, | ||
warn: function () {} | ||
@@ -9,0 +10,0 @@ }); |
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
60453
10
126
375