11import { size , map } from 'lodash'
22
3- const fixPath = path => ( ( path . substring ( 0 , 1 ) === '/' ) ? '' : '/' ) + path
4-
3+ /**
4+ * @description Fix path by adding "/" to path if needed
5+ * @param {String } path - Path string to fix
6+ * @return {String } path - Fixed path
7+ */
8+ export const fixPath = path =>
9+ ( ( path . substring ( 0 , 1 ) === '/' ) ? '' : '/' ) + path
10+
11+ /**
12+ * @description Convert Immutable Map to a Javascript object
13+ * @param {Object } data - Immutable Map to be converted to JS object
14+ * @return {Object } data - Javascript version of Immutable Map
15+ */
516export const toJS = data => {
617 if ( data && data . toJS ) {
718 return data . toJS ( )
@@ -10,6 +21,11 @@ export const toJS = data => {
1021 return data
1122}
1223
24+ /**
25+ * @description Convert parameter from Immutable Map to a Javascript object
26+ * @param {Object } data - Immutable Map to be converted to JS object
27+ * @return {Object } data - Javascript version of Immutable Map
28+ */
1329export const pathToJS = ( data , path , notSetValue ) => {
1430 if ( ! data ) {
1531 return notSetValue
@@ -24,6 +40,13 @@ export const pathToJS = (data, path, notSetValue) => {
2440 return data
2541}
2642
43+ /**
44+ * @description Convert parameter under "data" path of Immutable Map to a Javascript object
45+ * @param {Object } data - Immutable Map to be converted to JS object
46+ * @param {String } path - Path of parameter to load
47+ * @param {Object|String|Boolean } notSetValue - Value to return if value is not found
48+ * @return {Object } data - Javascript version of Immutable Map
49+ */
2750export const dataToJS = ( data , path , notSetValue ) => {
2851 if ( ! data ) {
2952 return notSetValue
@@ -40,6 +63,14 @@ export const dataToJS = (data, path, notSetValue) => {
4063 return data
4164}
4265
66+ /**
67+ * @description Load custom object from within store
68+ * @param {Object } data - Immutable Map from store to be converted to JS object
69+ * @param {String } path - Path of parameter to load
70+ * @param {String } customPath - Part of store from which to load
71+ * @param {Object|String|Boolean } notSetValue - Value to return if value is not found
72+ * @return {Object } data - Javascript version of custom path within Immutable Map
73+ */
4374export const customToJS = ( data , path , custom , notSetValue ) => {
4475 if ( ! ( data && data . getIn ) ) {
4576 return notSetValue
@@ -56,8 +87,14 @@ export const customToJS = (data, path, custom, notSetValue) => {
5687 return data
5788}
5889
90+ /**
91+ * @description Convert Immutable Map to a Javascript object
92+ * @param {Object } snapshot - Snapshot from store
93+ * @param {String } path - Path of snapshot to load
94+ * @return {Object } notSetValue - Value to return if snapshot is not found
95+ */
5996export const snapshotToJS = ( snapshot , path , notSetValue ) => {
60- if ( ! ( snapshot && snapshot . getIn ) ) {
97+ if ( ! snapshot ) {
6198 return notSetValue
6299 }
63100
@@ -72,6 +109,11 @@ export const snapshotToJS = (snapshot, path, notSetValue) => {
72109 return snapshot
73110}
74111
112+ /**
113+ * @description Detect whether items are loaded yet or not
114+ * @param {Object } item - Item to check loaded status of. A comma seperated list is also acceptable.
115+ * @return {Boolean } isLoaded - Whether or not item is loaded
116+ */
75117export const isLoaded = function ( ) {
76118 if ( ! arguments || ! arguments . length ) {
77119 return true
@@ -80,8 +122,21 @@ export const isLoaded = function () {
80122 return map ( arguments , a => a !== undefined ) . reduce ( ( a , b ) => a && b )
81123}
82124
125+ /**
126+ * @description Detect whether items are empty or not
127+ * @param {Object } item - Item to check loaded status of. A comma seperated list is also acceptable.
128+ * @return {Boolean } isEmpty - Whether or not item is empty
129+ */
83130export const isEmpty = data => {
84131 return ! ( data && size ( data ) )
85132}
86133
87- export default { toJS, pathToJS, dataToJS, snapshotToJS, customToJS, isLoaded, isEmpty }
134+ export default {
135+ toJS,
136+ pathToJS,
137+ dataToJS,
138+ snapshotToJS,
139+ customToJS,
140+ isLoaded,
141+ isEmpty
142+ }
0 commit comments