Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(deps): Remove check-types in favor of lodash #104

Merged
merged 1 commit into from
Jul 19, 2019

Conversation

orangejulius
Copy link
Member

@orangejulius orangejulius commented Jul 12, 2019

Since the creation of this module we have largely settled on lodash as our Javascript helper library of choice. This module was now using both lodash and check-types, which seemed excessive and inconsistent.

This change replaces all use of check-types with equivalent lodash functionality.

One notable conceptual change is regarding what is valid to store in a Variable object used as placeholder values in queries.

Initially, only scalar values were supported: non-empty strings, numbers, and booleans.

However, over time we found it useful to store first arrays and then even objects as variables, meaning its now clearer to enumerate what a Variable doesn't support storing: empty strings, null, and undefined

Since the creation of this module we have largely settled on lodash as
our Javascript helper library of choice. This module was now using both,
which seemed excessive and inconsistent.

This change replaces all use of `check-types` with equivalent lodash
functionality.

One notable conceptual change is regarding what is valid to store in a
`Variable` object used as placeholder values in queries.

Initially, only scalar values were supported: non-empty strings,
numbers, and booleans.

However, over time we found it useful to store first arrays and then
even objects as variables, meaning its now clearer to enumerate what a
`Variable` _doesn't_ support storing: empty strings, `null`, and
`undefined`
@missinglink
Copy link
Member

It's hard to get my head around this as the two libraries have quite different method names.

One thing I noticed is the use of _.isEmpty() for strings, I don't believe this is a correct usage of the function.

We need to be a little careful, the old syntax was actually much clearer in some cases IMHO and I'm a bit worried this refactor will introduce new bugs.


function Variable(){
this.$ = '';
}

Variable.prototype.set = function( val ){
if( !check.nonEmptyString(val) && !check.number(val) && !check.boolean(val) && !check.array(val) && !check.object(val)){
if( val === '' || val === null || val === undefined ){
throw new Error( 'invalid value, value must be valid js Variable' );
Copy link
Member

@missinglink missinglink Jul 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check for the nil number value NaN here also?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I was trying to preserve the existing behavior as much and as simply as possible, but NaN is definitely in the same boat as null and undefined as generally not useful.

On the other hand, and especially considering #13, which could be fixed by removing the restriction on storing an empty string, its kinda worth considering if we should filter out any values at all at this point. null and undefined at least would both cleanly serialize (to nothing) in JSON, so why not allow that if we allow so much else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge this as is but I'm definitely up for re-visiting allowed/disallowed values. I think we should allow '' to fix #13, figure out if we can stop allowing objects (which just sounds like a painful way to introduce weird behavior), and revisit null/undefined/NaN etc.

@@ -31,17 +32,19 @@ function VariableStore( vars ){
vs.var('foo').set('bar');
**/
VariableStore.prototype.var = function( key, val ){
if( !check.nonEmptyString( key ) ){
if( _.isEmpty( key ) || !_.isString( key) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this is correct, my understanding of _.isEmpty is that it's for collections, not strings.

You might have to write it isString(x) && !!x.length

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.isEmpty ends up checking .length, so it does work: https://github.com/lodash/lodash/blob/4.17.14/lodash.js#L11483-L11487

I agree that reading through the docs, it sounds like it shouldn't work, unless the definition of a "collection" is anything that has a length property.

Copy link
Member

@missinglink missinglink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, I trust your judgement on the impact (or lack thereof) of this :)

orangejulius added a commit to pelias/api that referenced this pull request Jul 19, 2019
@orangejulius
Copy link
Member Author

Acceptance tests all pass so I think we are safe :)

@orangejulius orangejulius merged commit 9a85233 into master Jul 19, 2019
@orangejulius orangejulius deleted the remove-check-types branch July 19, 2019 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants