Small utility for serialising JS objects to query strings for use in URLs

js-query-string-spec.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var querify = require("../lib/index.js");
  2. describe("Object -> Query String", function () {
  3. it("Should work for objects with strings", function () {
  4. var qs_object = {
  5. str: "mystr",
  6. second_str: "myotherstr"
  7. };
  8. expect(querify.convert(qs_object)).toBe("?str=mystr&second_str=myotherstr");
  9. });
  10. it("Should work with numbers", function () {
  11. var qs_object = {
  12. num: 5,
  13. othernum: 4000,
  14. neg_number: -20
  15. };
  16. expect(querify.convert(qs_object)).toBe("?num=5&othernum=4000&neg_number=-20");
  17. });
  18. it("Should work with booleans", function () {
  19. var qs_object = {
  20. bool_t: true,
  21. bool_f: false
  22. };
  23. expect(querify.convert(qs_object)).toBe("?bool_t=true&bool_f=false");
  24. });
  25. it("Should work with arrays", function () {
  26. var qs_object = {
  27. arr: [1,2,3],
  28. arr_two: [8,"string",true]
  29. };
  30. expect(querify.convert(qs_object)).toBe("?arr=%5B1%2C2%2C3%5D&arr_two=%5B8%2C%22string%22%2Ctrue%5D");
  31. });
  32. it("Should work with objects", function () {
  33. var qs_object = {
  34. obj1: {
  35. a: 1,
  36. b: "string"
  37. },
  38. obj2: {
  39. c: true,
  40. b: [1,2]
  41. }
  42. };
  43. expect(querify.convert(qs_object)).toBe("?obj1=%7B%22a%22%3A1%2C%22b%22%3A%22string%22%7D&obj2=%7B%22c%22%3Atrue%2C%22b%22%3A%5B1%2C2%5D%7D");
  44. });
  45. it("Should work with RegExp", function () {
  46. var qs_object = {
  47. reg: /[^a-zA-Z]/,
  48. reg2: /0/
  49. };
  50. expect(querify.convert(qs_object)).toBe("?reg=%2F%5B%5Ea-zA-Z%5D%2F&reg2=%2F0%2F");
  51. });
  52. });