- Notifications
You must be signed in to change notification settings - Fork47
Lightweight URL argument and parameter parser
License
stretchr/arg.js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Arg.js gives you quick and easy access to parameters in the URL.
- Download your own copy
- Package manager:NuGet, [Bower](http://bower.io/search/#!/search/javascript arguments)
- we would love to include more here, please send us Pull Requests
- BUG: Empty arrays result in extra
&&&&
- Simplified project structure and file names
- Minified version included in /dist
- Resolves decoding URL issues#17
- Fix for
Arg.query()in IE 8#19 - Use
hasOwnPropertywhen looping though arguments
- Added
Arg(key)shorter interface as well asArg.get(key). - Ignores undefined/empty keys and values.
- Cleans up edge cases (i.e. where paths are present in parse() calls etc).
- Will now optionally coerce a native type out of value if possible (i.e. Number, Boolean, undefined, etc). To not coerce, set
Arg.coerceMode = false - Better handling of complex objects that have mixed nested objects/arrays. See new test case added to test/spec/arg.js for an example object that was failing and is no longer failing.
- Added support for anchors in
Arg.url(path, params, anchorString)(i.e. no longer assumes they're variables if it's a string)
- Launch
The examples here assume this path:
page.html?name=Mat&address[0].city=London&address[0].country=UK&address[1].city=Boulder&address[1].country=US#?fromhash=trueArg("name")//= "Mat"It will get the value from both the query segment, and the hash segment.
Arg("fromhash")//= "true"Arg("address")//= [// { city: "London", country: "UK" },// { city: "Boulder", country: "US" }// ]Arg("address[0]")//= { city: "London", country: "UK" }Arg("address[0].city")//= "London"Arg("address[0].something", "Unknown")//= "Unknown"Arg.all()//= {// address: [// { city: "London", country: "UK" },// { city: "Boulder", country: "US" }// ],// fromhash: "true",// name: "Mat"// }Arg.all()gets all parameters (from the query and hash segments) in one object. Optionally, you can use thequeryorhashmethods to be specific.
Arg.query() gets an object made up of all the values in the query segment of the URL. The query segment is everything following the initial?, but before the# (if there is one.)
Arg.query()//= {// address: [// { city: "London", country: "UK" },// { city: "Boulder", country: "US" }// ],// name: "Mat"// }- Notice how the
fromhashvalue is missing.
Arg.hash() gets an object made up of all the values in the hash segment of the URL. The hash segment is anything following the#.
Arg.hash()//= {// fromhash: "true"// }Instead of using the current URL, you can be explicit by using theArg.parse method.
var myArgs = "name=Mat&company=Stretchr";Arg.parse(myArgs);//= {// name: "Mat",// company: "Stretchr"// }TheArg.url() function builds a URL, and has a few overloaded versions.
Passing just an object will generate a URL based on the current location, just changing the parameters.
Arg.url({name: "Mat", company: "Stretchr"});//= "path/to/current/page?name=Mat&company=Stretchr"If you setArg.urlUseHash = true, then the parameters will be placed in the hash segment of the new URL following the#? seperator:
Arg.urlUseHash = true;Arg.url({name: "Mat", company: "Stretchr"});//= "path/to/current/page#?name=Mat&company=Stretchr"Being explicit about a path in the first argument will use that location instead.
Arg.url("http://www.stretchr.com/", {name: "Mat", company: "Stretchr"});//= "http://www.stretchr.com/?name=Mat&company=Stretchr"If you want to use query and hash paremeters, pass a path and two objects.
Arg.url("http://www.stretchr.com/", {name: "Mat", company: "Stretchr"}, {comment: 123});//= "http://www.stretchr.com/?name=Mat&company=Stretchr#?comment=123";TheArg.stringify method lets you easily encode an object into a query string.
Arg.stringify({ name: "Mat" });//= name=MatArg.stringify({ one: { two: { three: 3 }}});//= one.two.three=3Arg.stringify({list:["one","two","three"]});//= list[0]=one&list[1]=two&list[2]=threeby Mat Ryer and Ryan QuinnCopyright (c) 2013 Stretchr, Inc.
Please consider promoting this project if you find it useful.
Permission is hereby granted, free of charge, to any person obtaining a copy of thissoftware and associated documentation files (the "Software"), to deal in the Softwarewithout restriction, including without limitation the rights to use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of the Software, and to permit personsto whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copiesor substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULARPURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLEFOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OROTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHERDEALINGS IN THE SOFTWARE.
About
Lightweight URL argument and parameter parser
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors11
Uh oh!
There was an error while loading.Please reload this page.