项目作者: rousan

项目描述 :
:zap: ES6 equivalent implimentation in ES5 for old JavaScript engines
高级语言: JavaScript
项目地址: git://github.com/rousan/es6-harmony.git
创建时间: 2017-06-07T19:00:10Z
项目社区:https://github.com/rousan/es6-harmony

开源协议:MIT License

下载


NPM version
NPM total downloads
License

ES6 Harmony

Provides an equivalent implementation of ES6(Harmony) in pure ES5 and creates an ES6 environment for old browsers and JavaScript Engines.

ES6 shims in pure ES5.

Install

NPM

Install it from npm and require it before any other modules:

  1. $ npm install --save es6-harmony
  1. var ES6 = require("es6-harmony");

CDN

If you prefer CDN, then just insert it into your HTML page on the top of other scripts:

<script src="https://cdn.jsdelivr.net/npm/es6-harmony/dist/es6-harmony.min.js"></script>

Examples

  1. "use strict";
  2. var ES6 = require("es6-harmony");
  3. var arr = [1, 2, 3];
  4. ES6.forOf(arr, function (v) {
  5. console.log(v);
  6. });
  7. // 1
  8. // 2
  9. // 3
  10. console.log(Array.from("Bar")); // [ 'B', 'a', 'r' ]
  11. console.log(Array.of(1, 2, 3)); // [ 1, 2, 3 ]
  12. console.log(Array.of.call(Object, 1, 2, 3)); // { [Number: 3] '0': 1, '1': 2, '2': 3, length: 3 }
  13. console.log([1, 2, 3].fill("Bar")); // [ 'Bar', 'Bar', 'Bar' ]
  14. console.log([1, 2, "bar"].find(function (v) {
  15. return typeof v === "number";
  16. }));
  17. // 1
  18. console.log([1, 2, "bar"].findIndex(function (v) {
  19. return typeof v === "number";
  20. }));
  21. // 0
  22. var it = arr.entries();
  23. console.log(it.next()); // { done: false, value: [ 0, 1 ] }
  24. console.log(it.next()); // { done: false, value: [ 1, 2 ] }
  25. console.log(it.next()); // { done: false, value: [ 2, 3 ] }
  26. console.log(it.next()); // { done: true, value: undefined }
  27. var map = new Map([[1, 2], ["bar", "baz"]]);
  28. console.log(map.size); // 2
  29. map.set(0, {});
  30. map.set(NaN, Object);
  31. console.log(map.has(NaN)); // true
  32. console.log(map.has(-0)); // true
  33. it = map.keys();
  34. console.log(it.next()); // { done: false, value: 1 }
  35. console.log(it.next()); // { done: false, value: 'bar' }
  36. console.log(it.next()); // { done: false, value: NaN }
  37. ES6.forOf(map, function (v) {
  38. console.log(v);
  39. });
  40. // [ 1, 2 ]
  41. // [ 'bar', 'baz' ]
  42. // [ 0, {} ]
  43. // [ NaN, [Function: Object] ]
  44. var set = new Set([1, 2, "bar"]);
  45. console.log(set.size); // 3
  46. set.add(-0);
  47. set.add(NaN);
  48. console.log(set.has(0)); // true
  49. console.log(set.has(NaN)); // true
  50. it = set.values();
  51. console.log(it.next()); // { done: false, value: 1 }
  52. console.log(it.next()); // { done: false, value: 2 }
  53. console.log(it.next()); // { done: false, value: 'bar' }
  54. ES6.forOf(set, function (v) {
  55. console.log(v);
  56. });
  57. // 1
  58. // 2
  59. // bar
  60. // -0
  61. // NaN
  62. var wm = new WeakMap();
  63. wm.set(Object, "object");
  64. wm.set({}, "normal object");
  65. console.log(wm.has(Object)); // true
  66. console.log(wm.has({})); // false
  67. var ws = new WeakSet();
  68. ws.add(Object);
  69. ws.add(Math);
  70. console.log(ws.has(Math)); // true
  71. console.log(ws.has(new Object())); // false
  72. console.log(Number.isNaN(NaN)); // true
  73. console.log(Number.isFinite(Infinity)); // false
  74. console.log(Number.isInteger(-11)); // true
  75. console.log(Number.isSafeInteger(Math.pow(2, 53) - 1)); // true
  76. console.log(Object.is(-0, 0)); // false
  77. console.log(Object.is(NaN, NaN)); // true
  78. var a = {};
  79. var b = {};
  80. Object.setPrototypeOf(a, b);
  81. console.log(Object.getPrototypeOf(a) === b); // true
  82. console.log(Object.assign({}, "bar")); // { '0': 'b', '1': 'a', '2': 'r' }
  83. var sym1 = Symbol("bar");
  84. var sym2 = Symbol("baz");
  85. a[sym1] = "This is symbol property";
  86. a[sym2] = "This is another symbol property";
  87. var allSymbols = Object.getOwnPropertySymbols(a);
  88. console.log(allSymbols.length); // 2
  89. console.log(allSymbols[0] === a); // false
  90. console.log(allSymbols[1] === b); // false
  91. console.log(String.fromCodePoint(0xFFFF, 0x10FFFF).length); // 3
  92. sym1 = Symbol("BAZ");
  93. sym2 = Symbol.for("BAZ");
  94. console.log(sym1 === Symbol("BAZ")); // false
  95. console.log(sym2 === Symbol.for("BAZ")); // true
  96. var obj = {
  97. length: 2,
  98. 0: "a",
  99. 1: 122
  100. };
  101. it = Array.prototype[Symbol.iterator].call(obj); // { done: false, value: 'a' }
  102. console.log(ES6.spreadOperator([]).spread("Bar", [1, 2, 3]).add(1, 2, 4).array()); // [ 'B', 'a', 'r', 1, 2, 3, 1, 2, 4 ]
  103. function sumArgs() {
  104. return Array.prototype.reduce.call(arguments, function (acc, currV) {
  105. return acc + currV;
  106. }, 0);
  107. }
  108. console.log(ES6.spreadOperator(sumArgs).spread([1, 2, 3]).add(11, 22).call()); // 39
  109. function Box(width, height) {
  110. this.width = width;
  111. this.height = height;
  112. }
  113. console.log(ES6.spreadOperator(Box).spread([1, 2.22]).new()); // Box { width: 1, height: 2.22 }
  114. console.log(it.next()); // { done: false, value: 'a' }
  115. var promise = new Promise(function (resolve, reject) {
  116. setTimeout(resolve, 0, 100);
  117. });
  118. promise.then(function (value) {
  119. console.log(value);
  120. return new Promise(function (resolve, reject) {
  121. setTimeout(resolve, 0, 110);
  122. });
  123. }).then(function (value) {
  124. console.log(value);
  125. return {
  126. then: function (resolve, reject) {
  127. setTimeout(reject, 0, "Error");
  128. }
  129. }
  130. }).catch(console.log.bind(console));
  131. // 100
  132. // 110
  133. // Error

Implemented

  • Array

    • Array.from()
    • Array.of()
    • Array.prototype.fill()
    • Array.prototype.find()
    • Array.prototype.findIndex()
    • Array.prototype.entries()
    • Array.prototype.keys()
    • Array.prototype.copyWithin()
    • Array.prototype.concat() (ES6 version, addition of @@isConcatSpreadable support)
    • Array.prototype[@@iterator]
  • Map

    • Map.prototype.size
    • Map.prototype.set()
    • Map.prototype.get()
    • Map.prototype.has()
    • Map.prototype.clear()
    • Map.prototype.delete()
    • Map.prototype.entries()
    • Map.prototype.forEach()
    • Map.prototype.keys()
    • Map.prototype.values()
    • Map.prototype[@@iterator]()
    • Map.prototype[@@toStringTag]()
  • Set

    • Set.prototype.size
    • Set.prototype.add()
    • Set.prototype.clear()
    • Set.prototype.delete()
    • Set.prototype.entries()
    • Set.prototype.forEach()
    • Set.prototype.has()
    • Set.prototype.keys()
    • Set.prototype.values()
    • Set.prototype[@@iterator]()
    • Set.prototype[@@toStringTag]()
  • WeakMap

    • WeakMap.prototype.delete()
    • WeakMap.prototype.get()
    • WeakMap.prototype.has()
    • WeakMap.prototype.set()
    • WeakMap.prototype[@@toStringTag]()
  • WeakSet

    • WeakSet.prototype.add()
    • WeakSet.prototype.delete()
    • WeakSet.prototype.has()
    • WeakSet.prototype[@@toStringTag]()
  • Number

    • Number.isNaN()
    • Number.isFinite()
    • Number.isInteger()
    • Number.parseInt()
    • Number.parseFloat()
    • Number.EPSILON
    • Number.MAX_SAFE_INTEGER
    • Number.MIN_SAFE_INTEGER
    • Number.isSafeIntger()
  • Object

    • Object.is()
    • Object.setPrototypeOf() (It assumes that Object.prototype.__proto__ accessor property already exists)
    • Object.assign()
    • Object.getOwnPropertySymbols
    • Object.prototype.toString() (ES6 version, addition of @@toStringTag support)
  • Promise

    • Promise.all()
    • Promise.race()
    • Promise.reject()
    • Promise.resolve()
    • Promise.prototype.then()
    • Promise.prototype.catch()
  • String

    • String.fromCodePoint()
    • String.prototype.codePointAt()
    • String.prototype.startsWith()
    • String.prototype.endsWith()
    • String.prototype.includes()
    • String.prototype.repeat()
  • Symbol

    • Symbol.for()
    • Symbol.keyFor()
    • Symbol.iterator
    • Symbol.hasInstance
    • Symbol.isConcatSpreadable
    • Symbol.toStringTag
    • Symbol.prototype.toString()
    • Symbol.prototype.valueOf()

Not Yet Implemented

Some features are not yet implemented, but can be implemented safely. Click here to see those features.

Limitation

Some ES6 features can not be implemented in ES5 natively like spread operator, for..of loop, ES6 version of instanceOf operator etc.
So this module exports an object named ES6 globally, that provides some approximate equivalent implementation of those features.

ES6 Object

This object provides,

  • isSymbol() (It can be used as equivalent API for typeof symbol === 'symbol')
  • instanceOf() (Provides ES6 version of instanceOf operator)
  • forOf() (This method behaves exactly same as for...of loop)
  • spreadOperator (Gives same functionality of the spread operator)

    Others utility methods,

  • isMap

  • isSet
  • isWeakMap
  • isWeakSet
  • isPromise

Contributing

Your PRs and stars are always welcome.

Please, try to follow:

  • Clone the repository.
  • Checkout develop branch.
  • Install dependencies.
  • Add your new features or fixes.
  • Build the project.
  1. $ git clone https://github.com/rousan/es6-harmony.git
  2. $ cd es6-harmony
  3. $ git checkout develop
  4. $ npm i
  5. $ npm run build