Skip to content

Commit 2edc17a

Browse files
committed
Added automatic polyfill version
1 parent 17898cd commit 2edc17a

8 files changed

+94
-10
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2015-2016 Rubén Norte <[email protected]>
1+
Copyright (c) 2015-2017 Rubén Norte <[email protected]>
22

33
MIT License
44

README.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ npm install es6-object-assign
1919

2020
The package is also available as a UMD module (compatible with AMD, CommonJS and exposing a global variable `ObjectAssign`) in `dist/object-assign.js` and `dist/object-assign.min.js` (833 bytes minified and gzipped).
2121

22-
## Usage
22+
The versions with automatic polyfilling are `dist/object-assign-auto.js` and `dist/object-assign-auto.min.js`.
2323

24-
As a polyfill, defining Object.assign() if it is not already defined:
24+
## Usage
2525

2626
**CommonJS**:
2727

@@ -30,21 +30,42 @@ As a polyfill, defining Object.assign() if it is not already defined:
3030
require('es6-object-assign').polyfill();
3131
var obj = Object.assign({}, { foo: 'bar' });
3232

33+
// Same version with automatic polyfilling
34+
require('es6-object-assign/auto');
35+
var obj = Object.assign({}, { foo: 'bar' });
36+
3337
// Or ponyfill, using a reference to the function without modifying globals
3438
var assign = require('es6-object-assign').assign;
3539
var obj = assign({}, { foo: 'bar' });
3640
```
3741

3842
**Globals**:
3943

44+
Manual polyfill:
45+
4046
```html
4147
<script src="<your-libs-directory>/object-assign.min.js"></script>
4248
<script>
4349
// Polyfill, modifying the global Object
4450
window.ObjectAssign.polyfill();
4551
var obj = Object.assign({}, { foo: 'bar' });
52+
</script>
53+
```
54+
55+
Automatic polyfill:
56+
57+
```html
58+
<script src="<your-libs-directory>/object-assign-auto.min.js"></script>
59+
<script>
60+
var obj = Object.assign({}, { foo: 'bar' });
61+
</script>
62+
```
4663

47-
// Or ponyfill, using a reference to the function without modifying globals
64+
Ponyfill, without modifying globals:
65+
66+
```html
67+
<script src="<your-libs-directory>/object-assign.min.js"></script>
68+
<script>
4869
var assign = window.ObjectAssign.assign;
4970
var obj = assign({}, { foo: 'bar' });
5071
</script>
@@ -54,7 +75,7 @@ var obj = assign({}, { foo: 'bar' });
5475

5576
The MIT License (MIT)
5677

57-
Copyright (c) 2015 Rubén Norte
78+
Copyright (c) 2017 Rubén Norte
5879

5980
Permission is hereby granted, free of charge, to any person obtaining a copy
6081
of this software and associated documentation files (the "Software"), to deal

auto.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
require('./index').polyfill();

dist/object-assign-auto.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2+
'use strict';
3+
4+
require('./index').polyfill();
5+
6+
},{"./index":2}],2:[function(require,module,exports){
7+
/**
8+
* Code refactored from Mozilla Developer Network:
9+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
10+
*/
11+
12+
'use strict';
13+
14+
function assign(target, firstSource) {
15+
if (target === undefined || target === null) {
16+
throw new TypeError('Cannot convert first argument to object');
17+
}
18+
19+
var to = Object(target);
20+
for (var i = 1; i < arguments.length; i++) {
21+
var nextSource = arguments[i];
22+
if (nextSource === undefined || nextSource === null) {
23+
continue;
24+
}
25+
26+
var keysArray = Object.keys(Object(nextSource));
27+
for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
28+
var nextKey = keysArray[nextIndex];
29+
var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
30+
if (desc !== undefined && desc.enumerable) {
31+
to[nextKey] = nextSource[nextKey];
32+
}
33+
}
34+
}
35+
return to;
36+
}
37+
38+
function polyfill() {
39+
if (!Object.assign) {
40+
Object.defineProperty(Object, 'assign', {
41+
enumerable: false,
42+
configurable: true,
43+
writable: true,
44+
value: assign
45+
});
46+
}
47+
}
48+
49+
module.exports = {
50+
assign: assign,
51+
polyfill: polyfill
52+
};
53+
54+
},{}]},{},[1]);

dist/object-assign-auto.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/object-assign.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js renamed to index.js

File renamed without changes.

package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
"name": "es6-object-assign",
33
"version": "1.0.3",
44
"description": "ECMAScript 2015 (ES6) Object.assign polyfill and ponyfill",
5-
"main": "src/index.js",
5+
"main": "index.js",
66
"scripts": {
7-
"compile": "browserify src/index.js -o dist/object-assign.js --standalone ObjectAssign",
8-
"compress": "uglifyjs dist/object-assign.js --compress --mangle > dist/object-assign.min.js",
7+
"compile": "npm run compile:manual && npm run compile:auto",
8+
"compile:manual": "browserify index.js -o dist/object-assign.js --standalone ObjectAssign",
9+
"compile:auto": "browserify auto.js -o dist/object-assign-auto.js",
10+
"compress": "npm run compress:manual && npm run compress:auto",
11+
"compress:manual": "uglifyjs dist/object-assign.js --compress --mangle > dist/object-assign.min.js",
12+
"compress:auto": "uglifyjs dist/object-assign-auto.js --compress --mangle > dist/object-assign-auto.min.js",
913
"build": "npm run compile && npm run compress"
1014
},
1115
"repository": {
@@ -33,7 +37,8 @@
3337
"uglify-js": "^2.4.21"
3438
},
3539
"files": [
36-
"src",
40+
"index.js",
41+
"auto.js",
3742
"dist"
3843
]
3944
}

0 commit comments

Comments
 (0)