examples.js 916 B

12345678910111213141516171819202122232425262728293031323334
  1. var StringMap = require("./stringmap");
  2. var sm1 = new StringMap();
  3. sm1.set("greeting", "yoyoma");
  4. sm1.set("check", true);
  5. sm1.set("__proto__", -1);
  6. console.log(sm1.has("greeting")); // true
  7. console.log(sm1.get("__proto__")); // -1
  8. sm1.remove("greeting");
  9. console.log(sm1.keys()); // [ 'check', '__proto__' ]
  10. console.log(sm1.values()); // [ true, -1 ]
  11. console.log(sm1.items()); // [ [ 'check', true ], [ '__proto__', -1 ] ]
  12. console.log(sm1.toString()); // {"check":true,"__proto__":-1}
  13. var sm2 = new StringMap({
  14. one: 1,
  15. two: 2,
  16. });
  17. console.log(sm2.map(function(value, key) {
  18. return value * value;
  19. })); // [ 1, 4 ]
  20. sm2.forEach(function(value, key) {
  21. // ...
  22. });
  23. console.log(sm2.isEmpty()); // false
  24. console.log(sm2.size()); // 2
  25. var sm3 = sm1.clone();
  26. sm3.merge(sm2);
  27. sm3.setMany({
  28. a: {},
  29. b: [],
  30. });
  31. console.log(sm3.toString()); // {"check":true,"one":1,"two":2,"a":{},"b":[],"__proto__":-1}