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

fix!: Properly handle function coercer in array of coercers #12

Merged
merged 3 commits into from Jan 30, 2022

Conversation

sttk
Copy link
Contributor

@sttk sttk commented Jun 30, 2019

When a type array contains 'function' or 'object', there are cases that this module returns incorrect result as follows. This pr fixes these bugs.

var normalize = require('value-or-function'), type, value;

type = ['function'];
value = function() {};
console.log(normalize(type, value));  // => undefined ... NG

type = ['object'];
value = {}
console.log(normalize(type, value));  // => {}
console.log(typeof normalize(type, value));  // => object ... OK

type = ['string', 'object'];
value = {}
console.log(normalize(type, value));  // => [object Object];
console.log(typeof normalize(type, value));  // => string ... NG

@phated phated self-requested a review October 21, 2020 21:01
@phated phated added this to In progress in v5 Oct 21, 2020
@phated
Copy link
Member

phated commented Jan 10, 2022

@sttk can you rebase/update this now that I merged #13 (sorry that I forgot to review it before that other PR 😭)

@sttk sttk force-pushed the bugfix_when_type_is_function_or_object branch from d83d0b2 to ffeab72 Compare January 16, 2022 07:20
@sttk
Copy link
Contributor Author

sttk commented Jan 16, 2022

@phated I've pushed new modifications from the current master branch.

@phated
Copy link
Member

phated commented Jan 16, 2022

Thanks!

I'm currently trying to decide if the following is the correct behavior:

type = ['string', 'object'];
value = {}
console.log(normalize(type, value));  // => [object Object];
console.log(typeof normalize(type, value));  // => string ... NG

A simple object, like {}, has a toString method, which should be called to coerce it to a string. I think this was a design choice, so maybe we shouldn't change it. If you want the object coercer to be applied first, you'd want to order the array ['object', 'string'] since they are resolved in order.

Do you have an example where that situation is needed?

Copy link
Member

@phated phated left a comment

Choose a reason for hiding this comment

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

@sttk I changed this PR a small amount, but I believe that I've handled most of the errors/bugs you encountered while keeping the expectation of array order.

I'm going to merge this now, but please review when you have time!

@phated phated changed the title Bugfix: Return incorrect result when type array contains "function" or "object" fix!: Properly handle function coercer in array of coercers Jan 30, 2022
@phated phated merged commit 1cf5dc7 into gulpjs:master Jan 30, 2022
@github-actions github-actions bot mentioned this pull request Jan 30, 2022
@sttk sttk deleted the bugfix_when_type_is_function_or_object branch March 19, 2022 08:50
@phated phated moved this from In progress to Done in v5 Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v5
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants