-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
Replace lodash utils #131
Replace lodash utils #131
Changes from all commits
3aef469
9724f2c
a5a6bf4
ddbfc5a
d76b216
974b7b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function identity(value) { | ||
return value; | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function isPlainObject(obj) { | ||
return obj ? typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype : false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A simple way to detect a pojo is to do Object.prototype.toString.call(object) === '[object Object]' or something like that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I won't work: var F = (function(){})
var f = new F
Object.prototype.toString.call(f) // [object Object] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ooflorent Are we trying to avoid the object above or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I thought that broke in some recent version, didn't it? Either way they don't officially support it yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I want from
I don't really care for edge cases like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gaearon I retract my earlier comment. You are correct |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function pick(obj, fn) { | ||
return Object.keys(obj).reduce((result, key) => { | ||
if (fn(obj[key])) { | ||
result[key] = obj[key]; | ||
} | ||
return result; | ||
}, {}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import expect from 'expect'; | ||
import identity from '../../src/utils/identity'; | ||
|
||
describe('Utils', () => { | ||
describe('identity', () => { | ||
it('should return first argument passed to it', () => { | ||
var test = { 'a': 1 }; | ||
expect(identity(test, 'test')).toBe(test); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import expect from 'expect'; | ||
import isPlainObject from '../../src/utils/isPlainObject'; | ||
|
||
describe('Utils', () => { | ||
describe('isPlainObject', () => { | ||
it('should return true only if plain object', () => { | ||
function Test() { | ||
this.prop = 1; | ||
} | ||
|
||
expect(isPlainObject(new Test())).toBe(false); | ||
expect(isPlainObject(new Date())).toBe(false); | ||
expect(isPlainObject([1, 2, 3])).toBe(false); | ||
expect(isPlainObject(null)).toBe(false); | ||
expect(isPlainObject()).toBe(false); | ||
expect(isPlainObject({ 'x': 1, 'y': 2 })).toBe(true); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import expect from 'expect'; | ||
import pick from '../../src/utils/pick'; | ||
|
||
describe('Utils', () => { | ||
describe('pick', () => { | ||
it('should return object with picked values', () => { | ||
const test = { 'name': 'lily', 'age': 20 }; | ||
expect(pick(test, x => typeof x === 'string')).toEqual({ 'name': 'lily' }); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually lodash's identity function is the same as yours but yes this does make sense to totally remove lodash as dependancy if this will be only one function needed from it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably best to put these all in a single file and export them individually that way you avoid the overhead of including each file, I think it adds ~3 lines and a bunch of bytes for every file you include.
This also has the benefit of keeping all the utils together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an aside: identity would then be something like:
export const identity = x => x;
which is just 😎There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@goatslacker Grouping these in one file sounds like a good idea to me however I worry it doesn't follow the style of the rest of utils of exporting one function per module (or as @taylorhakes mentions inlining some of them, especially simple ones like identity). Also, exporting arrow functions is pleasantly succinct but also another question of style :). @gaearon thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @goatslacker suggested, using an arrow function would be better because:
identity
is notnew
-ableidentity
has noprototype
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
I think it's just a matter whether we want to have it as a named function or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not the only implication of arrow functions 😉