Git Product home page Git Product logo

symbol-es6's Introduction

NPM version NPM total downloads Contributors License

Symbol-ES6

Provides support for ES6 Symbol API in ES5 for older JS environments i.e. older browsers or NodeJS.

ES6 Symbol polyfill in pure ES5.

Install

NPM

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

$ npm install --save symbol-es6
require("symbol-es6");

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/symbol-es6/dist/symbol-es6.min.js"></script>

Examples

"use strict";

var ES6 = require("symbol-es6");

console.log(Symbol("bar") === Symbol("bar")); //false

var sym = Symbol.for("bar");
var sym2 = Symbol("bar");

console.log(Symbol.for("bar") === sym); //true
console.log(sym === sym2); //false
console.log(Symbol.keyFor(sym)); //bar
console.log(Symbol.keyFor(sym2)); //undefined

console.log(Array.from("Hello")); //[ 'H', 'e', 'l', 'l', 'o' ]

var it = [1, 2].entries();

console.log(it.next()); //{ done: false, value: [ 0, 1 ] }
console.log(it.next()); //{ done: false, value: [ 1, 2 ] }
console.log(it.next()); //{ done: true, value: undefined }

it = [1, 2].keys();

console.log(it.next()); //{ done: false, value: 0 }
console.log(it.next()); //{ done: false, value: 1 }
console.log(it.next()); //{ done: true, value: undefined }

function Bar() {
}
console.log(Object.prototype.toString.call(new Bar())); //[object Object]

Bar.prototype[Symbol.toStringTag] = "Bar";

console.log(Object.prototype.toString.call(new Bar())); //[object Bar]

it = "Bar"[Symbol.iterator]();

console.log(it.next()); //{ done: false, value: 'B' }
console.log(it.next()); //{ done: false, value: 'a' }
console.log(it.next()); //{ done: false, value: 'r' }


console.log(ES6.isSymbol({})); //fasle
console.log(ES6.isSymbol(sym)); ///true

function Baz() {

}
console.log(ES6.instanceOf(89, Baz)); //false
Object.defineProperty(Baz, Symbol.hasInstance, {
   value: function (value) {
       return typeof value === "number";
   }
});
console.log(ES6.instanceOf(89, Baz)); //true

ES6.forOf([1, 2], function (v) {
    console.log(v);
});
//1
//2

console.log(ES6.spreadOperator([]).spread("Bar").array()); //[ 'B', 'a', 'r' ]

function TestSpread() {
    return Array.prototype.reduce.call(arguments, function (acc, currvalue) {
        return acc + currvalue;
    }, 0);
}

console.log(ES6.spreadOperator(TestSpread).spread([1, 2, 3, 4, 5]).call()); //15

function Student(name, roll) {
    this.name = name;
    this.roll = roll;
}

console.log(ES6.spreadOperator(Student).spread(["Ariyan", 10]).new().name); //Ariyan

Polyfills

  • Symbol

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

    • Function.prototype[@@hasInstance]()
  • Array

    • Array.prototype.concat() (ES6 version, addition of @@isConcatSpreadable support)
    • Array.prototype[@@iterator]()
    • Array.from()
    • Array.prototype.entries()
    • Array.prototype.keys()
  • Object

    • Object.prototype.toString() (ES6 version, addition of @@toStringTag support)
  • String

    • String.prototype[@@iterator]()

Limitation

Some ES6 features can't be implemented in ES5 natively like spread operator, for..of loop, ES6 version of instanceOf operator etc. So this module exports a 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 of: typeof symbol === 'symbol')
  • instanceOf() (Provides ES6 version of instanceOf)
  • forOf() (This method behaves exactly same as ES6 for...of loop)
  • spreadOperator (Gives same functionality of the spread operator of ES6)

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.
$ git clone https://github.com/rousan/symbol-es6.git
$ cd symbol-es6
$ git checkout develop
$ npm i
$ npm run build

symbol-es6's People

Contributors

rousan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

symbol-es6's Issues

polyfill always enabled

Bug: Polyfill is always enabled.
Expected behaviour: Polyfill is only enabled if needed for browser.

Cause:
var isES6Running = function() {
return false; /* Now 'false' for testing purpose */
};

Array.from iterator

const map = new Map().set("a", 1).set("b", 2).set(983, true);
console.log(Array.from(map.keys())); // output -> []

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.