pax_global_header00006660000000000000000000000064121741422210014506gustar00rootroot0000000000000052 comment=289e73112244fdd543b273be3618275a7ccee31f shallow-copy-0.0.1/000077500000000000000000000000001217414222100141255ustar00rootroot00000000000000shallow-copy-0.0.1/.travis.yml000066400000000000000000000000601217414222100162320ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" shallow-copy-0.0.1/LICENSE000066400000000000000000000020611217414222100151310ustar00rootroot00000000000000This software is released under the MIT license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. shallow-copy-0.0.1/example/000077500000000000000000000000001217414222100155605ustar00rootroot00000000000000shallow-copy-0.0.1/example/array.js000066400000000000000000000002661217414222100172400ustar00rootroot00000000000000var copy = require('../'); var xs = [ 3, 4, 5, { f: 6, g: 7 } ]; var dup = copy(xs); dup.unshift(1, 2); dup[5].g += 100; console.log('original: ', xs); console.log('copy: ', dup); shallow-copy-0.0.1/example/object.js000066400000000000000000000002571217414222100173700ustar00rootroot00000000000000var copy = require('../'); var obj = { a: 3, b: 4, c: [5,6] }; var dup = copy(obj); dup.b *= 111; dup.c.push(7); console.log('original: ', obj); console.log('copy: ', dup); shallow-copy-0.0.1/index.js000066400000000000000000000014361217414222100155760ustar00rootroot00000000000000module.exports = function (obj) { if (!obj || typeof obj !== 'object') return obj; var copy; if (isArray(obj)) { var len = obj.length; copy = Array(len); for (var i = 0; i < len; i++) { copy[i] = obj[i]; } } else { var keys = objectKeys(obj); copy = {}; for (var i = 0, l = keys.length; i < l; i++) { var key = keys[i]; copy[key] = obj[key]; } } return copy; }; var objectKeys = Object.keys || function (obj) { var keys = []; for (var key in obj) { if ({}.hasOwnProperty.call(obj, key)) keys.push(key); } return keys; }; var isArray = Array.isArray || function (xs) { return {}.toString.call(xs) === '[object Array]'; }; shallow-copy-0.0.1/package.json000066400000000000000000000015601217414222100164150ustar00rootroot00000000000000{ "name": "shallow-copy", "version": "0.0.1", "description": "make a shallow copy of an object or array", "main": "index.js", "dependencies": {}, "devDependencies": { "tape": "~1.0.4" }, "scripts": { "test": "tape test/*.js" }, "testling": { "files": "test/*.js", "browsers": [ "ie/8..latest", "ff/latest", "chrome/latest", "safari/latest", "opera/latest", "android/latest", "iphone/latest", "ipad/latest" ] }, "repository": { "type": "git", "url": "git://github.com/substack/shallow-copy.git" }, "homepage": "https://github.com/substack/shallow-copy", "keywords": [ "shallow", "copy", "data", "object", "array" ], "author": { "name": "James Halliday", "email": "mail@substack.net", "url": "http://substack.net" }, "license": "MIT" } shallow-copy-0.0.1/readme.markdown000066400000000000000000000023031217414222100171240ustar00rootroot00000000000000# shallow-copy make a shallow copy of an object or array [![testling badge](https://ci.testling.com/substack/shallow-copy.png)](https://ci.testling.com/substack/shallow-copy) [![build status](https://secure.travis-ci.org/substack/shallow-copy.png)](http://travis-ci.org/substack/shallow-copy) # example you can copy objects shallowly: ``` js var copy = require('shallow-copy'); var obj = { a: 3, b: 4, c: [5,6] }; var dup = copy(obj); dup.b *= 111; dup.c.push(7); console.log('original: ', obj); console.log('copy: ', dup); ``` and you can copy arrays shallowly: ``` js var copy = require('shallow-copy'); var xs = [ 3, 4, 5, { f: 6, g: 7 } ]; var dup = copy(xs); dup.unshift(1, 2); dup[5].g += 100; console.log('original: ', xs); console.log('copy: ', dup); ``` # methods ``` js var copy = require('shallow-copy') ``` ## copy(obj) Return a copy of the enumerable properties of the object `obj` without making copies of nested objects inside of `obj`. If `obj` is an array, the result will be an array. If `obj` is an object, the result will be an object. If `obj` is not an object, its value is returned. # install With [npm](https://npmjs.org) do: ``` npm install shallow-copy ``` # license MIT shallow-copy-0.0.1/test/000077500000000000000000000000001217414222100151045ustar00rootroot00000000000000shallow-copy-0.0.1/test/array.js000066400000000000000000000005171217414222100165630ustar00rootroot00000000000000var copy = require('../'); var test = require('tape'); test('array', function (t) { t.plan(2); var xs = [ 3, 4, 5, { f: 6, g: 7 } ]; var dup = copy(xs); dup.unshift(1, 2); dup[5].g += 100; t.deepEqual(xs, [ 3, 4, 5, { f: 6, g: 107 } ]); t.deepEqual(dup, [ 1, 2, 3, 4, 5, { f: 6, g: 107 } ]); }); shallow-copy-0.0.1/test/object.js000066400000000000000000000005071217414222100167120ustar00rootroot00000000000000var copy = require('../'); var test = require('tape'); test('object', function (t) { t.plan(2); var obj = { a: 3, b: 4, c: [5,6] }; var dup = copy(obj); dup.b *= 111; dup.c.push(7); t.deepEqual(obj, { a: 3, b: 4, c: [ 5, 6, 7 ] }); t.deepEqual(dup, { a: 3, b: 444, c: [ 5, 6, 7 ] }); });