Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
U
uritemplate-js
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
uritemplate-js
Commits
b03daea2
Commit
b03daea2
authored
Jun 15, 2012
by
fxa
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some minor code cleanups
parent
50a245c7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
93 deletions
+68
-93
README.md
README.md
+8
-5
package.json
package.json
+1
-1
src/uritemplate.js
src/uritemplate.js
+59
-87
No files found.
README.md
View file @
b03daea2
...
...
@@ -6,23 +6,25 @@ and can expand templates up to and including Level 4 in that specification.
It exposes a constructor function UriTemplate with the two methods:
*
(static) parse(uriTemplateText)
*
expand(variables)
*
(static) parse(uriTemplateText)
returning an instance of UriTemplate
*
expand(variables)
returning an string
Requirements
------------
You can use uritemplate.js in any
modern browsers
(Tested even with IE8 in IE7-Mode), see file demo.html.
You can use uritemplate.js in any
even not so modern browser
(Tested even with IE8 in IE7-Mode), see file demo.html.
But you can also use it with node:
npm install uritemplate
**npm install uritemplate**
and then:
var
UriTemplate = require('uritemplate'),
template;
template = UriTemplate.parse('{?query*}';
template.expand({query: {first: 1, second: 2}});
--> "?first
Param=firstValue&secondParam=secondValue
"
--> "?first
=1&second=2
"
Tests
-----
...
...
@@ -36,6 +38,7 @@ Comming soon
------------
*
A new method extract(uri), which tries to extract the variables from a given uri
*
Support of javascript's Date class
License
-------
...
...
package.json
View file @
b03daea2
...
...
@@ -23,5 +23,5 @@
"uritemplate-test/spec-examples-by-sections.json"
,
"uritemplate-test/spec-examples.json"
],
"version"
:
"0.1.
1
"
"version"
:
"0.1.
2
"
}
\ No newline at end of file
src/uritemplate.js
View file @
b03daea2
/*jshint browser:true, bitwise:true, curly:true, devel: true, eqeqeq:true, forin:true, immed:true, latedef:true, newcap:true, noarg:true, nonew:true, undef:true */
/*global module */
(
function
(
exportCallback
)
{
"
use strict
"
;
...
...
@@ -36,7 +34,7 @@
}
function
reduce
(
arrayOrObject
,
callback
,
initialValue
)
{
return
(
isArray
(
arrayOrObject
)
)
?
arrayReduce
(
arrayOrObject
,
callback
,
initialValue
)
:
objectReduce
(
arrayOrObject
,
callback
,
initialValue
);
return
isArray
(
arrayOrObject
)
?
arrayReduce
(
arrayOrObject
,
callback
,
initialValue
)
:
objectReduce
(
arrayOrObject
,
callback
,
initialValue
);
}
/**
...
...
@@ -68,11 +66,10 @@
// even the empty string is considered as defined
return
true
;
}
// else Object
for
(
propertyName
in
object
)
{
if
(
object
.
hasOwnProperty
(
propertyName
))
{
if
(
isDefined
(
object
[
propertyName
]))
{
return
true
;
}
if
(
object
.
hasOwnProperty
(
propertyName
)
&&
isDefined
(
object
[
propertyName
]))
{
return
true
;
}
}
return
false
;
...
...
@@ -83,7 +80,7 @@
}
function
isDigit
(
chr
)
{
return
(
chr
>=
'
0
'
&&
chr
<=
'
9
'
)
;
return
chr
>=
'
0
'
&&
chr
<=
'
9
'
;
}
function
isHexDigit
(
chr
)
{
...
...
@@ -141,73 +138,59 @@
// pass three chars to the result
result
+=
text
.
substr
(
index
,
3
);
index
+=
2
;
}
else
{
}
else
{
result
+=
'
%25
'
;
}
}
else
if
(
isUnreserved
(
chr
)
||
(
passReserved
&&
isReserved
(
chr
)))
{
result
+=
chr
;
}
else
{
result
+=
pctEncode
(
chr
);
result
+=
isUnreserved
(
chr
)
||
(
passReserved
&&
isReserved
(
chr
))
?
chr
:
pctEncode
(
chr
);
}
}
return
result
;
}
function
encodePassReserved
(
text
)
{
function
encodePassReserved
(
text
)
{
return
encode
(
text
,
true
);
}
function
Operator
(
symbol
,
encode
)
{
this
.
symbol
=
symbol
;
this
.
encode
=
encode
;
}
Operator
.
prototype
.
separator
=
function
()
{
return
(
this
.
symbol
===
'
?
'
)
?
'
&
'
:
(
this
.
symbol
===
''
||
this
.
symbol
===
'
+
'
||
this
.
symbol
===
'
#
'
)
?
'
,
'
:
this
.
symbol
;
};
Operator
.
prototype
.
named
=
function
()
{
return
this
.
symbol
===
'
;
'
||
this
.
symbol
===
'
&
'
||
this
.
symbol
===
'
?
'
;
};
Operator
.
prototype
.
ifEmpty
=
function
()
{
return
(
this
.
symbol
===
'
&
'
||
this
.
symbol
===
'
?
'
)
?
'
=
'
:
''
;
};
Operator
.
prototype
.
first
=
function
()
{
return
(
this
.
symbol
===
'
+
'
)
?
''
:
this
.
symbol
;
};
Operator
.
bySymbol
=
{
''
:
new
Operator
(
''
,
encode
),
'
+
'
:
new
Operator
(
'
+
'
,
encodePassReserved
),
'
#
'
:
new
Operator
(
'
#
'
,
encodePassReserved
),
'
.
'
:
new
Operator
(
'
.
'
,
encode
),
'
/
'
:
new
Operator
(
'
/
'
,
encode
),
'
;
'
:
new
Operator
(
'
;
'
,
encode
),
'
?
'
:
new
Operator
(
'
?
'
,
encode
),
'
&
'
:
new
Operator
(
'
&
'
,
encode
)
};
Operator
.
valueOf
=
function
(
chr
)
{
if
(
Operator
.
bySymbol
[
chr
])
{
return
Operator
.
bySymbol
[
chr
];
}
if
(
"
=,!@|
"
.
indexOf
(
chr
)
>=
0
)
{
throw
new
Error
(
'
Illegal use of reserved operator "
'
+
chr
+
'
"
'
);
}
return
Operator
.
bySymbol
[
''
];
};
var
UriTemplate
;
operators
=
(
function
()
{
var
bySymbol
=
{};
function
create
(
symbol
)
{
bySymbol
[
symbol
]
=
{
symbol
:
symbol
,
separator
:
(
symbol
===
'
?
'
)
?
'
&
'
:
(
symbol
===
''
||
symbol
===
'
+
'
||
symbol
===
'
#
'
)
?
'
,
'
:
symbol
,
named
:
symbol
===
'
;
'
||
symbol
===
'
&
'
||
symbol
===
'
?
'
,
ifEmpty
:
(
symbol
===
'
&
'
||
symbol
===
'
?
'
)
?
'
=
'
:
''
,
first
:
(
symbol
===
'
+
'
)
?
''
:
symbol
,
encode
:
(
symbol
===
'
+
'
||
symbol
===
'
#
'
)
?
encodePassReserved
:
encode
,
toString
:
function
()
{
return
this
.
symbol
;}
};
}
create
(
''
);
create
(
'
+
'
);
create
(
'
#
'
);
create
(
'
.
'
);
create
(
'
/
'
);
create
(
'
;
'
);
create
(
'
?
'
);
create
(
'
&
'
);
return
{
valueOf
:
function
(
chr
)
{
if
(
bySymbol
[
chr
])
{
return
bySymbol
[
chr
];
}
if
(
"
=,!@|
"
.
indexOf
(
chr
)
>=
0
)
{
throw
new
Error
(
'
Illegal use of reserved operator "
'
+
chr
+
'
"
'
);
}
return
bySymbol
[
''
];
}}
}());
UriTemplate
=
function
(
templateText
,
expressions
)
{
function
UriTemplate
(
templateText
,
expressions
)
{
this
.
templateText
=
templateText
;
this
.
experssions
=
expressions
;
}
;
}
UriTemplate
.
prototype
.
toString
=
function
()
{
return
this
.
templateText
;
...
...
@@ -240,12 +223,7 @@
}
}
else
{
if
(
isReserved
(
chr
)
||
isUnreserved
(
chr
))
{
result
+=
chr
;
}
else
{
result
+=
pctEncode
(
chr
);
}
result
+=
isReserved
(
chr
)
||
isUnreserved
(
chr
)
?
chr
:
pctEncode
(
chr
);
}
}
return
result
;
...
...
@@ -298,9 +276,9 @@
function
reduceNamedExploded
(
result
,
currentValue
,
currentKey
)
{
if
(
isDefined
(
currentValue
))
{
if
(
result
.
length
>
0
)
{
result
+=
operator
.
separator
()
;
result
+=
operator
.
separator
;
}
result
+=
(
valueIsArr
)
?
operator
.
encode
(
varspec
.
varname
)
:
operator
.
encode
(
currentKey
);
result
+=
(
valueIsArr
)
?
encodeLiteral
(
varspec
.
varname
)
:
operator
.
encode
(
currentKey
);
result
+=
'
=
'
+
operator
.
encode
(
currentValue
);
}
return
result
;
...
...
@@ -309,7 +287,7 @@
function
reduceUnnamedExploded
(
result
,
currentValue
,
currentKey
)
{
if
(
isDefined
(
currentValue
))
{
if
(
result
.
length
>
0
)
{
result
+=
operator
.
separator
()
;
result
+=
operator
.
separator
;
}
if
(
!
valueIsArr
)
{
result
+=
operator
.
encode
(
currentKey
)
+
'
=
'
;
...
...
@@ -327,24 +305,22 @@
continue
;
}
if
(
isFirstVarspec
)
{
result
+=
this
.
operator
.
first
()
;
result
+=
this
.
operator
.
first
;
isFirstVarspec
=
false
;
}
else
{
result
+=
this
.
operator
.
separator
()
;
result
+=
this
.
operator
.
separator
;
}
valueIsArr
=
isArray
(
value
);
if
(
typeof
value
===
"
string
"
||
typeof
value
===
"
number
"
||
typeof
value
===
"
boolean
"
)
{
value
=
value
.
toString
();
if
(
this
.
operator
.
named
()
)
{
result
+=
varspec
.
varname
;
if
(
this
.
operator
.
named
)
{
result
+=
encodeLiteral
(
varspec
.
varname
)
;
if
(
value
===
''
)
{
result
+=
this
.
operator
.
ifEmpty
()
;
result
+=
this
.
operator
.
ifEmpty
;
continue
;
}
else
{
result
+=
'
=
'
;
}
result
+=
'
=
'
;
}
if
(
varspec
.
maxLength
&&
value
.
length
>
varspec
.
maxLength
)
{
value
=
value
.
substr
(
0
,
varspec
.
maxLength
);
...
...
@@ -356,21 +332,19 @@
throw
new
Error
(
'
Prefix modifiers are not applicable to variables that have composite values. You tried to expand
'
+
this
+
"
with
"
+
JSON
.
stringify
(
value
));
}
else
if
(
!
varspec
.
exploded
)
{
if
(
operator
.
named
()
)
{
result
+=
varspec
.
varname
;
if
(
operator
.
named
)
{
result
+=
encodeLiteral
(
varspec
.
varname
)
;
if
(
!
isDefined
(
value
))
{
result
+=
this
.
operator
.
ifEmpty
()
;
result
+=
this
.
operator
.
ifEmpty
;
continue
;
}
else
{
result
+=
'
=
'
;
}
result
+=
'
=
'
;
}
result
+=
reduce
(
value
,
reduceUnexploded
,
''
);
}
else
{
// exploded and not string
result
+=
reduce
(
value
,
operator
.
named
()
?
reduceNamedExploded
:
reduceUnnamedExploded
,
''
);
result
+=
reduce
(
value
,
operator
.
named
?
reduceNamedExploded
:
reduceUnnamedExploded
,
''
);
}
}
return
result
;
...
...
@@ -385,7 +359,7 @@
varnameStart
=
null
,
maxLengthStart
=
null
,
index
,
chr
=
'
'
;
chr
;
function
closeVarname
()
{
varspec
=
{
varname
:
text
.
substring
(
varnameStart
,
index
),
exploded
:
false
,
maxLength
:
null
};
...
...
@@ -403,7 +377,7 @@
for
(
index
=
0
;
index
<
text
.
length
;
index
+=
chr
.
length
)
{
chr
=
text
[
index
];
if
(
index
===
0
)
{
operator
=
Operator
.
valueOf
(
chr
);
operator
=
operators
.
valueOf
(
chr
);
if
(
operator
.
symbol
!==
''
)
{
// first char is operator symbol. so we can continue
varnameStart
=
1
;
...
...
@@ -486,7 +460,6 @@
expressions
=
[],
braceOpenIndex
=
null
,
literalStart
=
0
;
for
(
index
=
0
;
index
<
uriTemplateText
.
length
;
index
+=
1
)
{
chr
=
uriTemplateText
.
charAt
(
index
);
if
(
literalStart
!==
null
)
{
...
...
@@ -549,4 +522,3 @@
}
}
));
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