Coder Social home page Coder Social logo

4stern / es6-shim Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paulmillr/es6-shim

0.0 2.0 0.0 2.13 MB

ECMAScript 6 (Harmony) compatibility shims for legacy JavaScript engines

Home Page: http://paulmillr.com

License: Other

JavaScript 98.39% HTML 1.61%

es6-shim's Introduction

ES6 Shim

Provides compatibility shims so that legacy JavaScript engines behave as closely as possible to ECMAScript 6 (Harmony).

Build Status dependency status dev dependency status

browser support

Sauce Test Status

Installation

If you want to use it in browser:

  • Just include es6-shim before your scripts.
  • Include es5-shim especially if your browser doesn't support ECMAScript 5 - but every JS engine requires the es5-shim to correct broken implementations, so it's strongly recommended to always include it.

For node.js, io.js, or any npm-managed workflow (this is the recommended method):

npm install es6-shim

Alternative methods:

  • component install paulmillr/es6-shim if you’re using component(1).
  • bower install es6-shim if you’re using Bower.

In both browser and node you may also want to include unorm; see the String.prototype.normalize section for details.

Safe shims

Math functions’ accuracy is 1e-11.

  • Reflect

    • apply()
    • construct()
    • defineProperty()
    • deleteProperty()
    • enumerate()
    • get()
    • getOwnPropertyDescriptor()
    • getPrototypeOf()
    • has()
    • isExtensible()
    • ownKeys()
    • preventExtensions()
    • set()
    • setPrototypeOf()
  • String.prototype Annex B HTML methods These methods are part of "Annex B", which means that although they are a defacto standard, you shouldn't use them. None the less, the es6-shim provides them:

    • anchor()
    • big()
    • blink()
    • bold()
    • fixed()
    • fontcolor()
    • fontsize()
    • italics()
    • link()
    • small()
    • strike()
    • sub()
    • sup()

Subclassing

The Map, Set, and Promise implementations are subclassable. You should use the following pattern to create a subclass in ES5 which will continue to work in ES6:

function MyPromise(exec) {
  Promise.call(this, exec);
  // ...
}
Object.setPrototypeOf(MyPromise, Promise);
MyPromise.prototype = Object.create(Promise.prototype, {
  constructor: { value: MyPromise }
});

String.prototype.normalize

Including a proper shim for String.prototype.normalize would increase the size of this library by a factor of more than 4. So instead we recommend that you install the unorm package alongside es6-shim if you need String.prototype.normalize. See paulmillr#134 for more discussion.

WeakMap shim

It is not possible to implement WeakMap in pure javascript. The es6-collections implementation doesn't hold values strongly, which is critical for the collection. es6-shim decided to not include an incorrect shim.

WeakMap has a very unusual use-case so you probably won't need it at all (use simple Map instead).

Getting started

'abc'.startsWith('a') // true
'abc'.endsWith('a') // false
'john alice'.includes('john') // true
'123'.repeat(2)     // '123123'

Object.is(NaN, NaN) // Fixes ===. 0 isnt -0, NaN is NaN
Object.assign({a: 1}, {b: 2}) // {a: 1, b: 2}

Number.isNaN('123') // false. isNaN('123') will give true.
Number.isFinite('asd') // false. Global isFinite() will give true.
// Tests if value is a number, finite,
// >= -9007199254740992 && <= 9007199254740992 and floor(value) === value
Number.isInteger(2.4) // false.

Math.sign(400) // 1, 0 or -1 depending on sign. In this case 1.

[5, 10, 15, 10].find(function (item) {return item / 2 === 5;}) // 10
[5, 10, 15, 10].findIndex(function (item) {return item / 2 === 5;}) // 1

// Replacement for `{}` key-value storage.
// Keys can be anything.
var map = new Map();
map.set('John', 25);
map.set('Alice', 400);
map.set(['meh'], 555);
assert(map.get(['meh']) === undefined); // undefined because you need to use exactly the same object.
map.delete('Alice');
map.keys();
map.values();
assert(map.size === 2);

// Useful for storing unique items.
var set = new Set();
set.add(1);
set.add(5);
assert(set.has(1) === true);
assert(set.has(4) === false);
set.delete(5);

// Promises, see
// http://www.slideshare.net/domenicdenicola/callbacks-promises-and-coroutines-oh-my-the-evolution-of-asynchronicity-in-javascript
// https://github.com/petkaantonov/bluebird/#what-are-promises-and-why-should-i-use-them
Promise.resolve(5).then(function (value) {
  if ( ... ) throw new Error("whoops!");
  // do some stuff
  return anotherPromise();
}).catch(function (e) {
  // any errors thrown asynchronously end up here
});

Other stuff:

es6-shim's People

Contributors

amasad avatar arlolra avatar cscott avatar daniel-hug avatar david-risney avatar dekajp avatar dylanlukes avatar fb55 avatar forbeslindesay avatar hemanth avatar jussi-kalliokoski avatar kvantetore avatar ljharb avatar matchdav avatar mathiasbynens avatar mikemcelroy avatar ndhoule avatar ngsankha avatar nowaker avatar nuxodin avatar orald avatar outcoldman avatar paulmillr avatar phenomnomnominal avatar pkozlowski-opensource avatar rwaldron avatar smikes avatar subzey avatar victorystick avatar yaffle avatar

Watchers

 avatar  avatar

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.