-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
default to array when update keys are integers
- Loading branch information
1 parent
7a35404
commit c33ae8f
Showing
5 changed files
with
61 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import isEmpty from './isEmpty'; | ||
|
||
function isInt(value) { | ||
if (isNaN(value)) { | ||
return false; | ||
} | ||
const x = parseFloat(value); | ||
return (x | 0) === x; | ||
} | ||
|
||
function isArrayUpdate(updates) { | ||
for (const updateKey of Object.keys(updates)) { | ||
if (!isInt(updateKey)) { return false; } | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function arrayOrObject(updates) { | ||
if (!isEmpty(updates) && isArrayUpdate(updates)) { | ||
return []; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
function defaultObject(object, updates) { | ||
if (typeof object === 'undefined' || object === null) { | ||
return arrayOrObject(updates); | ||
} | ||
|
||
return object; | ||
} | ||
|
||
export default defaultObject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function isEmpty(object) { | ||
return !Object.keys(object).length; | ||
} | ||
|
||
export default isEmpty; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters