Fix object coercion.

This commit is contained in:
John-David Dalton
2017-04-16 15:30:42 -05:00
parent e2941dda3b
commit aa5e1b2fe0
7 changed files with 20 additions and 10 deletions

View File

@@ -11,11 +11,12 @@ const hasOwnProperty = Object.prototype.hasOwnProperty
* @returns {Array} Returns the array of property names. * @returns {Array} Returns the array of property names.
*/ */
function baseKeys(object) { function baseKeys(object) {
object = Object(object)
if (!isPrototype(object)) { if (!isPrototype(object)) {
return Object.keys(Object(object)) return Object.keys(object)
} }
const result = [] const result = []
for (const key in Object(object)) { for (const key in object) {
if (hasOwnProperty.call(object, key) && key != 'constructor') { if (hasOwnProperty.call(object, key) && key != 'constructor') {
result.push(key) result.push(key)
} }

View File

@@ -20,7 +20,9 @@
* // => false * // => false
*/ */
function everyValue(object, predicate) { function everyValue(object, predicate) {
const props = Object.keys(Object(object)) object = Object(object)
const props = Object.keys(object)
for (const key of props) { for (const key of props) {
if (!predicate(object[key], key, object)) { if (!predicate(object[key], key, object)) {
return false return false

View File

@@ -17,8 +17,10 @@
* // => [5, 10] * // => [5, 10]
*/ */
function filterObject(object, predicate) { function filterObject(object, predicate) {
object = Object(object)
const result = [] const result = []
Object.keys(Object(object)).forEach((key) => {
Object.keys(object).forEach((key) => {
const value = object[key] const value = object[key]
if (predicate(value, key, object)) { if (predicate(value, key, object)) {
result.push(value) result.push(value)

View File

@@ -24,9 +24,8 @@
* // => Logs 'a' then 'b' (iteration order is not guaranteed). * // => Logs 'a' then 'b' (iteration order is not guaranteed).
*/ */
function forOwn(object, iteratee) { function forOwn(object, iteratee) {
if (object != null) { object = Object(object)
Object.keys(Object(object)).forEach((key) => iteratee(object[key], key, object)) Object.keys(object).forEach((key) => iteratee(object[key], key, object))
}
} }
export default forOwn export default forOwn

View File

@@ -18,8 +18,10 @@
* // => { 'a1': 1, 'b2': 2 } * // => { 'a1': 1, 'b2': 2 }
*/ */
function mapKey(object, iteratee) { function mapKey(object, iteratee) {
object = Object(object)
const result = {} const result = {}
Object.keys(Object(object)).forEach((key) => {
Object.keys(object).forEach((key) => {
const value = object[key] const value = object[key]
result[iteratee(value, key, object)] = value result[iteratee(value, key, object)] = value
}) })

View File

@@ -21,8 +21,10 @@
* // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed) * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
*/ */
function mapValue(object, iteratee) { function mapValue(object, iteratee) {
object = Object(object)
const result = {} const result = {}
Object.keys(Object(object)).forEach((key) => {
Object.keys(object).forEach((key) => {
result[key] = iteratee(object[key], key, object) result[key] = iteratee(object[key], key, object)
}) })
return result return result

View File

@@ -15,7 +15,9 @@
* // => true * // => true
*/ */
function someValues(object, predicate) { function someValues(object, predicate) {
const props = Object.keys(Object(object)) object = Object(object)
const props = Object.keys(object)
for (const key of props) { for (const key of props) {
if (predicate(object[key], key, object)) { if (predicate(object[key], key, object)) {
return true return true