, , . mylib.search . , , , window.location.search . :
getParameterByName: (name) =>
console.log("name: #{name}")
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]")
regexS = "[\\?&]" + name + "=([^&#]*)"
regex = new RegExp(regexS)
results = regex.exec(@getWindowLocationSearch())
if(results == null)
return ""
else
return decodeURIComponent(results[1].replace(/\+/g, " "))
getWindowLocationSearch:() =>
window.location.search
:
describe "Data tests", () ->
it "Should parse parameter from localhost url", () ->
goodUrl = "http://localhost:3333/?token=val1"
Data::getWindowLocationSearch = () -> return goodUrl
unit = new Data()
result = unit.getParameterByName("token")
expect(result).toBe("val1")
, Coffeescript, javascript :
it("Should parse parameter from localhost url", function() {
var goodUrl, result, unit;
goodUrl = "http://localhost:3333/?token=val1";
Data.prototype.getWindowLocationSearch = function() {
return goodUrl;
};
unit = new Data();
result = unit.getParameterByName("token");
expect(result).toBe("val1");
return expect(true).toBe(true);
});
Javascript. , . .