Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dataService bug fixes and documentation
- Loading branch information
1 parent
a57d74d
commit 9bea1c3
Showing
10 changed files
with
706 additions
and
482 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,31 @@ | ||
# types for generating test data: null, number, string, boolean, timestamp, <array>[], <object>, <objectName in Specification> | ||
class Generator extends Service | ||
self = null | ||
constructor: -> | ||
self = @ | ||
|
||
number: (min = 0, max = 100) -> | ||
random = Math.random() * (max - min) + min | ||
Math.floor(random) | ||
|
||
ids: {} | ||
id: (name = '') -> | ||
self.ids[name] ?= 0 | ||
self.ids[name]++ | ||
|
||
boolean: -> Math.random() < 0.5 | ||
|
||
timestamp: (after = Date.now()) -> | ||
date = new Date(after + self.number(1, 1000000)) | ||
Math.floor(date.getTime() / 1000) | ||
|
||
string: (length) -> | ||
if length? then length++ | ||
self.number(100, Number.MAX_VALUE).toString(36).substring(0, length) | ||
|
||
array: (fn, args...) -> | ||
times = self.number(1, 10) | ||
array = [] | ||
for i in [1..times] | ||
array.push fn(args...) | ||
return array |
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
116 changes: 66 additions & 50 deletions
116
www/data_module/src/services/dataUtils/dataUtils.service.coffee
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 |
---|---|---|
@@ -1,52 +1,68 @@ | ||
class DataUtils extends Service | ||
constructor: (SPECIFICATION) -> | ||
return new class dataUtilsService | ||
# capitalize first word | ||
capitalize: (string) -> | ||
string[0].toUpperCase() + string[1..].toLowerCase() | ||
|
||
# capitalize first word | ||
capitalize: (string) -> | ||
string[0].toUpperCase() + string[1..].toLowerCase() | ||
|
||
# returns the type of the endpoint | ||
type: (arg) -> | ||
a = @copyOrSplit(arg) | ||
a = a.filter (e) -> e isnt '*' | ||
# if the argument count is even, the last argument is an id | ||
if a.length % 2 is 0 then a.pop() | ||
a.pop() | ||
|
||
# singularize the type name | ||
singularType: (arg) -> | ||
@type(arg).replace(/s$/, '') | ||
|
||
socketPath: (arg) -> | ||
a = @copyOrSplit(arg) | ||
# if the argument count is even, the last argument is an id | ||
stars = ['*'] | ||
# is it odd? | ||
if a.length % 2 is 1 then stars.push('*') | ||
a.concat(stars).join('/') | ||
|
||
restPath: (arg) -> | ||
a = @copyOrSplit(arg) | ||
a = a.filter (e) -> e isnt '*' | ||
a.join('/') | ||
|
||
endpointPath: (arg) -> | ||
# if the argument count is even, the last argument is an id | ||
a = @copyOrSplit(arg) | ||
a = a.filter (e) -> e isnt '*' | ||
# is it even? | ||
if a.length % 2 is 0 then a.pop() | ||
a.join('/') | ||
|
||
copyOrSplit: (arrayOrString) -> | ||
if angular.isArray(arrayOrString) | ||
# return a copy | ||
arrayOrString[..] | ||
else if angular.isString(arrayOrString) | ||
# split the string to get an array | ||
arrayOrString.split('/') | ||
else | ||
throw new TypeError("Parameter 'arrayOrString' must be a array or a string, not #{typeof arrayOrString}") | ||
|
||
unWrap: (data, path) -> | ||
type = @type(path) | ||
data[type] | ||
# returns the type of the endpoint | ||
type: (arg) -> | ||
a = @copyOrSplit(arg) | ||
a = a.filter (e) -> e isnt '*' | ||
# if the argument count is even, the last argument is an id | ||
if a.length % 2 is 0 then a.pop() | ||
a.pop() | ||
|
||
# singularize the type name | ||
singularType: (arg) -> | ||
@type(arg).replace(/s$/, '') | ||
|
||
socketPath: (arg) -> | ||
a = @copyOrSplit(arg) | ||
# if the argument count is even, the last argument is an id | ||
stars = ['*'] | ||
# is it odd? | ||
if a.length % 2 is 1 then stars.push('*') | ||
a.concat(stars).join('/') | ||
|
||
restPath: (arg) -> | ||
a = @copyOrSplit(arg) | ||
a = a.filter (e) -> e isnt '*' | ||
a.join('/') | ||
|
||
endpointPath: (arg) -> | ||
# if the argument count is even, the last argument is an id | ||
a = @copyOrSplit(arg) | ||
a = a.filter (e) -> e isnt '*' | ||
# is it even? | ||
if a.length % 2 is 0 then a.pop() | ||
a.join('/') | ||
|
||
copyOrSplit: (arrayOrString) -> | ||
if angular.isArray(arrayOrString) | ||
# return a copy | ||
arrayOrString[..] | ||
else if angular.isString(arrayOrString) | ||
# split the string to get an array | ||
arrayOrString.split('/') | ||
else | ||
throw new TypeError("Parameter 'arrayOrString' must be a array or a string, not #{typeof arrayOrString}") | ||
|
||
unWrap: (data, path) -> | ||
type = @type(path) | ||
type = SPECIFICATION[type]?.restField or type | ||
data[type] | ||
|
||
parse: (object) -> | ||
for k, v of object | ||
try | ||
object[k] = angular.fromJson(v) | ||
catch error then # ignore | ||
return object | ||
|
||
numberOrString: (str = null) -> | ||
# if already a number | ||
if angular.isNumber(str) then return str | ||
# else parse string to integer | ||
number = parseInt str, 10 | ||
if !isNaN(number) then number else str |
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
Oops, something went wrong.