Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Challonge
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
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
MyCard
Challonge
Commits
b7429ce5
Commit
b7429ce5
authored
Dec 09, 2013
by
Aaron Tidwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixing camel-underscore and back
parent
1aac05e9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
62 deletions
+42
-62
lib/api/client.js
lib/api/client.js
+37
-16
lib/api/properties-to-convert.js
lib/api/properties-to-convert.js
+0
-41
test/test.js
test/test.js
+5
-5
No files found.
lib/api/client.js
View file @
b7429ce5
...
...
@@ -12,7 +12,13 @@ var errorHandler = require('./error-handler');
*/
var
Client
=
exports
.
Client
=
function
(
options
)
{
this
.
options
=
options
;
if
(
!
this
.
options
.
version
)
{
this
.
options
.
version
=
1
;
}
//defaults - todo convert to an object merge
if
(
!
this
.
options
.
version
)
{
this
.
options
.
version
=
1
;
}
if
(
!
this
.
options
.
massageProperties
)
{
this
.
options
.
massageProperties
=
true
;
}
this
.
setSubdomain
(
this
.
options
.
subdomain
);
...
...
@@ -30,7 +36,7 @@ function serializeProperties(obj) {
var
serializedProperties
=
[];
for
(
var
prop
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
prop
))
{
if
(
typeof
(
obj
[
prop
])
===
'
object
'
)
{
if
(
typeof
(
obj
[
prop
])
===
'
object
'
&&
obj
[
prop
]
!==
null
)
{
for
(
var
attr
in
obj
[
prop
])
{
compiledParams
+=
'
&
'
;
compiledParams
+=
prop
+
'
[
'
+
attr
+
'
]=
'
+
encodeURIComponent
(
obj
[
prop
][
attr
]);
...
...
@@ -48,26 +54,36 @@ function serializeProperties(obj) {
// resources generate props internal to https requests
var
propertiesToDelete
=
[
'
callback
'
,
'
path
'
,
'
method
'
];
// all the stuff we have to convert camelCase to under_scores
var
propertiesToConvert
=
require
(
'
./properties-to-convert
'
);
function
camelToUnderscore
(
str
)
{
return
str
.
replace
(
/
\W
+/g
,
'
-
'
)
.
replace
(
/
([
a-z
\d])([
A-Z
])
/g
,
'
$1_$2
'
).
toLowerCase
();
return
str
.
replace
(
/
\W
+/g
,
'
-
'
)
.
replace
(
/
([
a-z
\d])([
A-Z
])
/g
,
'
$1_$2
'
).
toLowerCase
();
}
function
UnderscoreToCamel
(
str
)
{
return
str
.
replace
(
/_
([
a-z
])
/g
,
function
(
g
)
{
return
g
[
1
].
toUpperCase
();
});
}
function
convertPropertyCamelCaseToUnderscore
(
obj
)
{
function
convertProperties
(
obj
,
converionFunction
)
{
//determine which we want to check with to see if we should convert
var
checkRegex
=
converionFunction
===
UnderscoreToCamel
?
/_/
:
/
[
A-Z
]
/
;
console
.
log
(
checkRegex
);
for
(
var
prop
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
prop
))
{
//objects recurse
if
(
typeof
obj
[
prop
]
===
'
object
'
)
{
obj
[
camelToUnderscore
(
prop
)]
=
convertPropertyCamelCaseToUnderscore
(
obj
[
prop
]);
if
(
typeof
obj
[
prop
]
===
'
object
'
&&
obj
[
prop
]
!==
null
)
{
console
.
log
(
'
recursing
'
,
prop
)
obj
[
converionFunction
(
prop
)]
=
convertProperties
(
obj
[
prop
],
converionFunction
);
}
//otherwise, if the underscore version is in the list we convert it
else
if
(
propertiesToConvert
.
indexOf
(
camelToUnderscore
(
prop
))
!==
-
1
)
{
obj
[
camelToUnderscore
(
prop
)]
=
obj
[
prop
];
else
if
(
prop
.
search
(
checkRegex
)
>
-
1
)
{
obj
[
converionFunction
(
prop
)]
=
obj
[
prop
];
//remove it
console
.
log
(
'
removing
'
,
prop
)
delete
obj
[
prop
];
}
console
.
log
(
'
failed
'
,
prop
,
prop
.
search
(
checkRegex
))
//otherwise leave it alone
}
}
...
...
@@ -91,11 +107,12 @@ Client.prototype.makeRequest = function(obj) {
var
path
=
obj
.
path
;
var
method
=
obj
.
method
;
// massage camel to underscore
// normalize the rest of the properties
obj
=
convertProperties
(
obj
,
camelToUnderscore
);
// Add on the api key
obj
.
api_key
=
this
.
options
.
get
(
'
apiKey
'
);
//convert for url
// normalize the rest of the properties
obj
=
convertPropertyCamelCaseToUnderscore
(
obj
);
console
.
log
(
obj
)
//serialize the properties
...
...
@@ -141,6 +158,10 @@ Client.prototype.makeRequest = function(obj) {
// 200 ok
if
(
self
.
options
.
get
(
'
format
'
)
==
'
json
'
)
{
resData
=
JSON
.
parse
(
resData
);
if
(
self
.
options
.
get
(
'
massageProperties
'
))
{
console
.
log
(
'
converting
'
,
resData
);
resData
=
convertProperties
(
resData
,
UnderscoreToCamel
);
}
}
callback
(
null
,
resData
);
//no error, so no err object
});
...
...
lib/api/properties-to-convert.js
deleted
100644 → 0
View file @
1aac05e9
//all the stuff we have to convert camelCase to under_scores
var
propertiesToConvert
=
[
'
create_after
'
,
'
created_before
'
,
'
include_participants
'
,
'
include_matches
'
,
'
tournament_type
'
,
'
open_signup
'
,
'
hold_third_place_match
'
,
'
pts_for_match_win
'
,
'
pts_for_match_tie
'
,
'
pts_for_game_win
'
,
'
pts_for_game_tie
'
,
'
pts_for_bye
'
,
'
swiss_rounds
'
,
'
ranked_by
'
,
'
rr_pts_for_match_win
'
,
'
rr_pts_for_match_tie
'
,
'
rr_pts_for_game_win
'
,
'
rr_pts_for_game_tie
'
,
'
accept_attachments
'
,
'
hide_forum
'
,
'
show_rounds
'
,
'
notify_users_when_matches_open
'
,
'
notify_users_when_the_tournament_ends
'
,
'
sequential_pairings
'
,
'
signup_cap
'
,
'
challonge_username
'
,
'
participant_id
'
,
'
scores_csv
'
,
'
winner_id
'
,
'
player1_votes
'
,
'
player2_votes
'
,
];
module
.
exports
=
propertiesToConvert
;
\ No newline at end of file
test/test.js
View file @
b7429ce5
...
...
@@ -6,7 +6,7 @@ var client = challonge.createClient({
version
:
1
,
});
var
tourneyName
=
'
nodeapitestcamel
'
;
var
tourneyName
=
'
nodeapite
3
stcamel
'
;
function
index
()
{
client
.
tournaments
.
index
({
...
...
@@ -108,9 +108,8 @@ function pindex() {
function
pcreate
()
{
client
.
participants
.
create
({
id
:
tourneyName
,
participantId
:
'
arbitraryid
'
,
participant
:
{
name
:
'
Tidwell
'
name
:
'
Tidwell
345678901239331
'
},
callback
:
function
(
err
,
data
){
if
(
err
)
{
console
.
log
(
err
);
return
;
}
...
...
@@ -191,8 +190,8 @@ function mupdate() {
id
:
tourneyName
,
matchId
:
15606254
,
match
:
{
scores
_c
sv
:
'
3-0
'
,
winner
_i
d
:
10847219
scores
C
sv
:
'
3-0
'
,
winner
I
d
:
10847219
},
callback
:
function
(
err
,
data
){
if
(
err
)
{
console
.
log
(
err
);
return
;
}
...
...
@@ -215,6 +214,7 @@ create();
//pindex();
//pcreate();
//pshow();
//pupdate();
//pdestroy();
...
...
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