Skip to content

Commit

Permalink
don't replace "extend", use "extendext" instead
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Dec 18, 2014
1 parent 9fefa31 commit b29d765
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# jQuery.extend-enhanced
# jQuery.extendext

[![Bower version](https://badge.fury.io/bo/jQuery.extend-enhanced.svg)](http://badge.fury.io/bo/jQuery.extend-enhanced)
[![Bower version](https://badge.fury.io/bo/jquery-extendext.svg)](http://badge.fury.io/bo/jquery-extendext)

jQuery.extend with configurable behaviour for arrays.

Expand All @@ -26,12 +26,12 @@ Other deep merging utilities I found either have the same behaviour or perform b

## Usage

When loading **jQuery.extend-enhanced.js** the default `$.extend` function will be replaced by my own implementation with the exact same behaviour if not additional config is provided.
**jQuery.extendext.js** contains a new `$.extendext` function with the exact same behaviour as `$.extend` if not additional config is provided.

The difference is that it accepts a optional second string argument to specify how arrays should be merged.

```js
jQuery.extend([deep ,][arrayMode ,] target, object1 [, objectN ] )
jQuery.extendext([deep ,][arrayMode ,] target, object1 [, objectN ] )
```

* **deep** _boolean_ — If true, the merge becomes recursive (aka. deep copy).
Expand All @@ -53,7 +53,7 @@ var config = {
operators: ['OR', 'XOR']
};

config = $.extend(true, 'replace', {}, DEFAULTS, config);
config = $.extendext(true, 'replace', {}, DEFAULTS, config);

assert.deepEqual(config, {
operators: ['OR', 'XOR']
Expand All @@ -73,7 +73,7 @@ var config = {
operators: ['OR', 'XOR']
};

config = $.extend(true, 'concat', {}, DEFAULTS, config);
config = $.extendext(true, 'concat', {}, DEFAULTS, config);

assert.deepEqual(config, {
operators: ['AND', 'OR', 'XOR', 'OR', 'XOR']
Expand All @@ -96,7 +96,7 @@ var config = {
operators: ['XOR', 'NAND']
};

config = $.extend(true, 'extend', {}, DEFAULTS, config);
config = $.extendext(true, 'extend', {}, DEFAULTS, config);

assert.deepEqual(config, {
operators: ['AND', 'OR', 'XOR', 'NAND']
Expand All @@ -105,7 +105,7 @@ assert.deepEqual(config, {

### "default" mode

Same as if the parameter is not provided.
Same as `$.extend`.

```js
var DEFAULTS = {
Expand All @@ -116,9 +116,15 @@ var config = {
operators: ['OR', 'XOR']
};

config = $.extend(true, 'default', {}, DEFAULTS, config);
config = $.extendext(true, 'default', {}, DEFAULTS, config);

assert.deepEqual(config, {
operators: ['OR', 'XOR', 'XOR']
}) // true;
```
```

## Tests

A QUnit test suite is provided in `tests` directory.

`$.extendext` is tested against core jQuery tests for `$.extend` and `nrf110/deepmerge` tests (with the difference that extendext, like extend, modifies the first argument where deepmerge does not touch it).
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"name": "jQuery.extend-enhanced",
"name": "jquery-extendext",
"version": "0.1.0",
"description": "jQuery.extend with configurable behaviour for arrays",
"authors": [{
"name": "Damien \"Mistic\" Sorel",
"email": "[email protected]",
"homepage": "http://www.strangeplanet.fr"
}],
"main": "jQuery.extend-enchanced.js",
"main": "jQuery.extendext.js",
"keywords": [
"jQuery",
"extend",
"arrays"
],
"license": "MIT",
"homepage": "https://github.com/mistic100/jQuery.extend-enhanced",
"homepage": "https://github.com/mistic100/jQuery.extendext",
"dependencies": {
"jquery": ">=1.9.1"
},
Expand Down
14 changes: 6 additions & 8 deletions jQuery.extend-enhanced.js → jQuery.extendext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* jQuery.extend-enhanced
* jQuery.extendext
*
* Copyright 2014 Damien "Mistic" Sorel (http://www.strangeplanet.fr)
* Licensed under MIT (http://opensource.org/licenses/MIT)
Expand All @@ -10,9 +10,7 @@
(function($){
"use strict";

jQuery._original_extend = jQuery.extend;

jQuery.extend = function() {
jQuery.extendext = function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
Expand Down Expand Up @@ -56,18 +54,18 @@

switch (arrayMode) {
case 'concat':
target = clone.concat( jQuery.extend( deep, 'default', [], options ) );
target = clone.concat( jQuery.extend( deep, [], options ) );
break;

case 'replace':
target = jQuery.extend( deep, 'default', [], options );
target = jQuery.extend( deep, [], options );
break;

case 'extend':
options.forEach(function(e, i) {
if (typeof e === 'object') {
var type = jQuery.isArray(e) ? [] : {};
clone[i] = jQuery.extend( deep, arrayMode, clone[i] || type, e );
clone[i] = jQuery.extendext( deep, arrayMode, clone[i] || type, e );

} else if (clone.indexOf(e) === -1) {
clone.push(e);
Expand Down Expand Up @@ -102,7 +100,7 @@
}

// Never move original objects, clone them
target[ name ] = jQuery.extend( deep, arrayMode, clone, copy );
target[ name ] = jQuery.extendext( deep, arrayMode, clone, copy );

// Don't bring in undefined values
} else if ( copy !== undefined ) {
Expand Down
3 changes: 2 additions & 1 deletion tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../bower_components/qunit/qunit/qunit.js"></script>

<script src="../jQuery.extend-enhanced.js"></script>
<script src="../jQuery.extendext.js"></script>
<script>$.extend = $.extendext; // for the tests</script>

<script src="tests.js"></script>
<script src="jquery-core-tests.js"></script>
Expand Down
11 changes: 3 additions & 8 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
module('$.extend-enhanced');

test('Function loaded', function() {
ok($.extend != $.fn.extend, 'Don\'t modify "fn.extend"');
ok($._original_extend == $.fn.extend, 'Store "_original_extend"');
});

test('New modes', function() {
var a, b, o;

Expand All @@ -23,7 +18,7 @@ test('New modes', function() {
quux: 5
};

deepEqual($.extend(true, 'extend', a, b), o, 'extend');
deepEqual($.extendext(true, 'extend', a, b), o, 'extend');

a = {
foo: { bar: 3 },
Expand All @@ -38,7 +33,7 @@ test('New modes', function() {
array: [1, 2, 4, 1, 4, 5]
};

deepEqual($.extend(true, 'concat', a, b), o, 'concat');
deepEqual($.extendext(true, 'concat', a, b), o, 'concat');

a = {
foo: { bar: 3 },
Expand All @@ -53,5 +48,5 @@ test('New modes', function() {
array: [4, 5]
};

deepEqual($.extend(true, 'replace', a, b), o, 'replace');
deepEqual($.extendext(true, 'replace', a, b), o, 'replace');
});

0 comments on commit b29d765

Please sign in to comment.