Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
GammaJS
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
3
Issues
3
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
GSC Libraries
GammaJS
Commits
6a212996
Commit
6a212996
authored
May 23, 2018
by
Ben Galloway
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make object reduce method DRY
parent
24c62263
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
42 deletions
+32
-42
dist/util.js
dist/util.js
+16
-21
src/util.js
src/util.js
+16
-21
No files found.
dist/util.js
View file @
6a212996
...
...
@@ -14,29 +14,30 @@ var _xmlJs2 = _interopRequireDefault(_xmlJs);
function
_interopRequireDefault
(
obj
)
{
return
obj
&&
obj
.
__esModule
?
obj
:
{
default
:
obj
};
}
// UTILITY FUNCTIONS
const
objectReduce
=
(
callback
,
objectToReduce
,
methodSpecs
=
null
)
=>
{
return
Object
.
keys
(
objectToReduce
).
reduce
((
distilledObject
,
key
)
=>
{
return
_extends
({},
distilledObject
,
callback
({
fieldContents
:
objectToReduce
[
key
],
fieldKey
:
key
,
methodSpecs
}));
},
{});
};
// INPUT WRAPPERS
const
parseInputField
=
(
fieldContents
,
fieldKey
)
=>
{
const
parseInputField
=
(
{
fieldContents
,
fieldKey
}
)
=>
{
if
(
fieldKey
===
"
GammaInputType
"
||
fieldKey
===
"
GammaResponseType
"
)
return
{
_attributes
:
{
"
xsi:type
"
:
fieldContents
}
};
if
(
fieldContents
.
constructor
===
Object
)
return
{
[
fieldKey
]:
sanitise
(
fieldContents
)
};
if
(
fieldContents
.
constructor
===
Object
)
return
{
[
fieldKey
]:
objectReduce
(
parseInputField
,
fieldContents
)
};
if
(
Array
.
isArray
(
fieldContents
))
{
const
arrayOfParsedObjects
=
fieldContents
.
map
(
singleInputObject
=>
{
return
sanitise
(
singleInputObject
);
return
objectReduce
(
parseInputField
,
singleInputObject
);
});
return
{
[
fieldKey
]:
arrayOfParsedObjects
};
}
return
{
[
fieldKey
]:
fieldContents
};
};
const
sanitise
=
callObject
=>
{
const
sanitised
=
Object
.
keys
(
callObject
).
reduce
((
distilledObject
,
key
)
=>
{
return
_extends
({},
distilledObject
,
parseInputField
(
callObject
[
key
],
key
));
},
{});
return
sanitised
;
};
const
getXml
=
exports
.
getXml
=
(
callObject
,
method
)
=>
{
const
sanitisedCallObject
=
sanitise
(
callObject
);
const
sanitisedCallObject
=
objectReduce
(
parseInputField
,
callObject
);
const
fullCall
=
{
_declaration
:
{
_attributes
:
{
version
:
"
1.0
"
,
encoding
:
"
utf-8
"
}
},
XMLRequest
:
{
...
...
@@ -55,7 +56,7 @@ const getXml = exports.getXml = (callObject, method) => {
// OUTPUT PARSERS
const
parseResponseField
=
(
fieldContents
,
fieldKey
,
methodSpecs
)
=>
{
const
parseResponseField
=
(
{
fieldContents
,
fieldKey
,
methodSpecs
}
)
=>
{
if
(
fieldContents
.
constructor
===
Object
&&
Object
.
keys
(
fieldContents
).
length
===
0
)
return
{
[
fieldKey
]:
""
};
if
(
fieldKey
===
"
xs:schema
"
)
return
{};
if
(
fieldKey
===
"
mResults
"
&&
fieldContents
.
hasOwnProperty
(
"
diffgr:diffgram
"
))
return
{
...
...
@@ -66,11 +67,9 @@ const parseResponseField = (fieldContents, fieldKey, methodSpecs) => {
if
(
fieldKey
===
"
_attributes
"
&&
fieldContents
.
hasOwnProperty
(
"
diffgr:id
"
)
&&
fieldContents
.
hasOwnProperty
(
"
msdata:rowOrder
"
))
return
{
GammaResponseType
:
"
VisitorStatsDay
"
};
if
(
fieldContents
.
hasOwnProperty
(
"
_text
"
))
return
{
[
fieldKey
]:
fieldContents
[
"
_text
"
]
};
if
(
fieldContents
.
hasOwnProperty
(
"
anyType
"
))
return
{
[
fieldKey
]:
processAnyType
(
fieldContents
.
anyType
,
methodSpecs
)
};
if
(
fieldContents
.
hasOwnProperty
(
fieldKey
))
return
{
[
fieldKey
]:
parseResponseField
(
fieldContents
[
fieldKey
],
fieldKey
,
methodSpecs
)
};
if
(
fieldContents
.
hasOwnProperty
(
fieldKey
))
return
{
[
fieldKey
]:
parseResponseField
(
{
fieldContents
:
fieldContents
[
fieldKey
],
fieldKey
,
methodSpecs
}
)
};
if
(
fieldContents
.
constructor
===
Object
&&
Object
.
keys
(
fieldContents
).
length
!==
0
)
{
const
formattedContents
=
Object
.
keys
(
fieldContents
).
reduce
((
distilledObject
,
key
)
=>
{
return
_extends
({},
distilledObject
,
parseResponseField
(
fieldContents
[
key
],
key
,
methodSpecs
));
},
{});
const
formattedContents
=
objectReduce
(
parseResponseField
,
fieldContents
,
methodSpecs
);
return
{
[
fieldKey
]:
formattedContents
};
}
return
{
[
fieldKey
]:
fieldContents
};
...
...
@@ -81,11 +80,7 @@ const processAnyType = (anyType, methodSpecs, internal = false) => {
const
anyTypeArray
=
Array
.
isArray
(
anyType
)
?
anyType
:
new
Array
(
anyType
);
const
reduced
=
anyTypeArray
.
map
(
verboseObject
=>
{
const
keys
=
Object
.
keys
(
verboseObject
);
const
distilled
=
keys
.
reduce
((
distilledObject
,
key
)
=>
{
return
_extends
({},
distilledObject
,
parseResponseField
(
verboseObject
[
key
],
key
,
methodSpecs
));
},
{});
return
distilled
;
return
objectReduce
(
parseResponseField
,
verboseObject
,
methodSpecs
);
});
const
result
=
!
internal
&&
methodSpecs
.
returnType
===
false
&&
reduced
.
length
===
1
?
reduced
[
0
]
:
reduced
;
// Only return an array if necessary
...
...
src/util.js
View file @
6a212996
// Utility parsing and wrapping functions for calls to Gamma
import
convert
from
"
xml-js
"
;
// UTILITY FUNCTIONS
const
objectReduce
=
(
callback
,
objectToReduce
,
methodSpecs
=
null
)
=>
{
return
Object
.
keys
(
objectToReduce
).
reduce
((
distilledObject
,
key
)
=>
{
return
{
...
distilledObject
,
...
callback
({
fieldContents
:
objectToReduce
[
key
],
fieldKey
:
key
,
methodSpecs
})
};
},
{});
};
// INPUT WRAPPERS
const
parseInputField
=
(
fieldContents
,
fieldKey
)
=>
{
const
parseInputField
=
(
{
fieldContents
,
fieldKey
}
)
=>
{
if
(
fieldKey
===
"
GammaInputType
"
||
fieldKey
===
"
GammaResponseType
"
)
return
{
_attributes
:
{
"
xsi:type
"
:
fieldContents
}
};
if
(
fieldContents
.
constructor
===
Object
)
return
{
[
fieldKey
]:
sanitise
(
fieldContents
)
};
if
(
fieldContents
.
constructor
===
Object
)
return
{
[
fieldKey
]:
objectReduce
(
parseInputField
,
fieldContents
)
};
if
(
Array
.
isArray
(
fieldContents
))
{
const
arrayOfParsedObjects
=
fieldContents
.
map
((
singleInputObject
)
=>
{
return
sanitise
(
singleInputObject
);
return
objectReduce
(
parseInputField
,
singleInputObject
);
});
return
{
[
fieldKey
]:
arrayOfParsedObjects
};
}
return
{
[
fieldKey
]:
fieldContents
};
};
const
sanitise
=
(
callObject
)
=>
{
const
sanitised
=
Object
.
keys
(
callObject
).
reduce
((
distilledObject
,
key
)
=>
{
return
{
...
distilledObject
,
...
parseInputField
(
callObject
[
key
],
key
)
};
},
{});
return
sanitised
;
};
export
const
getXml
=
(
callObject
,
method
)
=>
{
const
sanitisedCallObject
=
sanitise
(
callObject
);
const
sanitisedCallObject
=
objectReduce
(
parseInputField
,
callObject
);
const
fullCall
=
{
_declaration
:
{
_attributes
:
{
version
:
"
1.0
"
,
encoding
:
"
utf-8
"
}
},
XMLRequest
:
{
...
...
@@ -43,7 +44,7 @@ export const getXml = (callObject, method) => {
// OUTPUT PARSERS
const
parseResponseField
=
(
fieldContents
,
fieldKey
,
methodSpecs
)
=>
{
const
parseResponseField
=
(
{
fieldContents
,
fieldKey
,
methodSpecs
}
)
=>
{
if
(
fieldContents
.
constructor
===
Object
&&
Object
.
keys
(
fieldContents
).
length
===
0
)
return
{
[
fieldKey
]:
""
};
if
(
fieldKey
===
"
xs:schema
"
)
return
{};
if
(
fieldKey
===
"
mResults
"
&&
fieldContents
.
hasOwnProperty
(
"
diffgr:diffgram
"
))
...
...
@@ -69,11 +70,9 @@ const parseResponseField = (fieldContents, fieldKey, methodSpecs) => {
if
(
fieldContents
.
hasOwnProperty
(
"
anyType
"
))
return
{
[
fieldKey
]:
processAnyType
(
fieldContents
.
anyType
,
methodSpecs
)
};
if
(
fieldContents
.
hasOwnProperty
(
fieldKey
))
return
{
[
fieldKey
]:
parseResponseField
(
fieldContents
[
fieldKey
],
fieldKey
,
methodSpecs
)
};
return
{
[
fieldKey
]:
parseResponseField
(
{
fieldContents
:
fieldContents
[
fieldKey
],
fieldKey
,
methodSpecs
}
)
};
if
(
fieldContents
.
constructor
===
Object
&&
Object
.
keys
(
fieldContents
).
length
!==
0
)
{
const
formattedContents
=
Object
.
keys
(
fieldContents
).
reduce
((
distilledObject
,
key
)
=>
{
return
{
...
distilledObject
,
...
parseResponseField
(
fieldContents
[
key
],
key
,
methodSpecs
)
};
},
{});
const
formattedContents
=
objectReduce
(
parseResponseField
,
fieldContents
,
methodSpecs
);
return
{
[
fieldKey
]:
formattedContents
};
}
return
{
[
fieldKey
]:
fieldContents
};
...
...
@@ -84,11 +83,7 @@ const processAnyType = (anyType, methodSpecs, internal = false) => {
const
anyTypeArray
=
Array
.
isArray
(
anyType
)
?
anyType
:
new
Array
(
anyType
);
const
reduced
=
anyTypeArray
.
map
((
verboseObject
)
=>
{
const
keys
=
Object
.
keys
(
verboseObject
);
const
distilled
=
keys
.
reduce
((
distilledObject
,
key
)
=>
{
return
{
...
distilledObject
,
...
parseResponseField
(
verboseObject
[
key
],
key
,
methodSpecs
)
};
},
{});
return
distilled
;
return
objectReduce
(
parseResponseField
,
verboseObject
,
methodSpecs
);
});
const
result
=
!
internal
&&
methodSpecs
.
returnType
===
false
&&
reduced
.
length
===
1
?
reduced
[
0
]
:
reduced
;
// Only return an array if necessary
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment