├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended" 4 | ], 5 | 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | 13 | "parserOptions":{ 14 | "ecmaVersion": 9, 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "modules": true, 18 | "experimentalObjectRestSpread": true 19 | } 20 | }, 21 | 22 | "globals": { 23 | "document": false, 24 | "navigator": false, 25 | "window": false 26 | }, 27 | 28 | "rules": { 29 | "accessor-pairs": 2, 30 | "arrow-spacing": [2, { "before": true, "after": true }], 31 | "block-spacing": [2, "always"], 32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 33 | "comma-dangle": [2, "never"], 34 | "comma-spacing": [2, { "before": false, "after": true }], 35 | "comma-style": [2, "last"], 36 | "constructor-super": 2, 37 | "curly": [2, "multi-line"], 38 | "dot-location": [2, "property"], 39 | "eol-last": 2, 40 | "eqeqeq": [2, "allow-null"], 41 | "generator-star-spacing": [2, { "before": true, "after": true }], 42 | "handle-callback-err": [2, "^(err|error)$" ], 43 | "indent": [2, 2, { "SwitchCase": 1 }], 44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 45 | "keyword-spacing": [2, { "before": true, "after": true }], 46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 47 | "new-parens": 2, 48 | "no-array-constructor": 2, 49 | "no-caller": 2, 50 | "no-class-assign": 2, 51 | "no-cond-assign": 2, 52 | "no-const-assign": 2, 53 | "no-control-regex": 2, 54 | "no-debugger": 2, 55 | "no-delete-var": 2, 56 | "no-dupe-args": 2, 57 | "no-dupe-class-members": 2, 58 | "no-dupe-keys": 2, 59 | "no-duplicate-case": 2, 60 | "no-empty-character-class": 2, 61 | "no-eval": 2, 62 | "no-ex-assign": 2, 63 | "no-extend-native": 2, 64 | "no-extra-bind": 2, 65 | "no-extra-boolean-cast": 2, 66 | "no-extra-parens": [2, "functions"], 67 | "no-fallthrough": 2, 68 | "no-floating-decimal": 2, 69 | "no-func-assign": 2, 70 | "no-implied-eval": 2, 71 | "no-inner-declarations": [2, "functions"], 72 | "no-invalid-regexp": 2, 73 | "no-irregular-whitespace": 2, 74 | "no-iterator": 2, 75 | "no-label-var": 2, 76 | "no-labels": 2, 77 | "no-lone-blocks": 2, 78 | "no-mixed-spaces-and-tabs": 2, 79 | "no-multi-spaces": 2, 80 | "no-multi-str": 2, 81 | "no-multiple-empty-lines": [2, { "max": 1 }], 82 | "no-native-reassign": 0, 83 | "no-negated-in-lhs": 2, 84 | "no-new": 2, 85 | "no-new-func": 2, 86 | "no-new-object": 2, 87 | "no-new-require": 2, 88 | "no-new-wrappers": 2, 89 | "no-obj-calls": 2, 90 | "no-octal": 2, 91 | "no-octal-escape": 2, 92 | "no-proto": 0, 93 | "no-redeclare": 2, 94 | "no-regex-spaces": 2, 95 | "no-return-assign": 2, 96 | "no-self-compare": 2, 97 | "no-sequences": 2, 98 | "no-shadow-restricted-names": 2, 99 | "no-spaced-func": 2, 100 | "no-sparse-arrays": 2, 101 | "no-this-before-super": 2, 102 | "no-throw-literal": 2, 103 | "no-trailing-spaces": 0, 104 | "no-undef": 2, 105 | "no-undef-init": 2, 106 | "no-unexpected-multiline": 2, 107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 108 | "no-unreachable": 2, 109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 110 | "no-useless-call": 0, 111 | "no-with": 2, 112 | "one-var": [0, { "initialized": "never" }], 113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 114 | "padded-blocks": [0, "never"], 115 | "quotes": [2, "single", "avoid-escape"], 116 | "radix": 2, 117 | "semi": [2, "always"], 118 | "semi-spacing": [2, { "before": false, "after": true }], 119 | "space-before-blocks": [2, "always"], 120 | "space-before-function-paren": [2, "never"], 121 | "space-in-parens": [2, "never"], 122 | "space-infix-ops": 2, 123 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 125 | "use-isnan": 2, 126 | "valid-typeof": 2, 127 | "wrap-iife": [2, "any"], 128 | "yoda": [2, "never"] 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | .idea 4 | .vscode 5 | *.sublime-* 6 | 7 | # test related, or directories generated by tests 8 | test/actual 9 | actual 10 | coverage 11 | .nyc* 12 | 13 | # npm 14 | node_modules 15 | npm-debug.log 16 | 17 | # yarn 18 | yarn.lock 19 | yarn-error.log 20 | 21 | # misc 22 | _gh_pages 23 | _draft 24 | _drafts 25 | bower_components 26 | vendor 27 | temp 28 | tmp 29 | TODO.md 30 | package-lock.json -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '10' 9 | - '9' 10 | - '8' 11 | - '7' 12 | - '6' 13 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## About 2 | 3 | > A symbol is a unique and immutable data type and may be used as an identifier for object properties. The symbol object is an implicit object wrapper for the symbol primitive data type. - [Mozilla Developer docs for Symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) 4 | 5 | ## Usage 6 | 7 | ```js 8 | const assignSymbols = require('{%= name %}'); 9 | let target = {}; 10 | 11 | // add a symbol to object "one" 12 | let one = {}; 13 | let symbolOne = Symbol('aaa'); 14 | one[symbolOne] = 'bbb'; 15 | 16 | // add a symbol to object "two" 17 | let two = {}; 18 | let symbolTwo = Symbol('ccc'); 19 | two[symbolTwo] = 'ddd'; 20 | 21 | // assign symbols from objects "one" and "two" to object "target" 22 | assignSymbols(target, one, two); 23 | 24 | console.log(target[symbolOne]); //=> 'bbb' 25 | console.log(target[symbolTwo]); //=> 'ddd' 26 | ``` 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-present, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # assign-symbols [![NPM version](https://img.shields.io/npm/v/assign-symbols.svg?style=flat)](https://www.npmjs.com/package/assign-symbols) [![NPM monthly downloads](https://img.shields.io/npm/dm/assign-symbols.svg?style=flat)](https://npmjs.org/package/assign-symbols) [![NPM total downloads](https://img.shields.io/npm/dt/assign-symbols.svg?style=flat)](https://npmjs.org/package/assign-symbols) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/assign-symbols.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/assign-symbols) 2 | 3 | > Assign the enumerable es6 Symbol properties from one or more objects to the first object passed on the arguments. Can be used as a supplement to other extend, assign or merge methods as a polyfill for the Symbols part of the es6 Object.assign method. 4 | 5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install --save assign-symbols 13 | ``` 14 | 15 | ## About 16 | 17 | > A symbol is a unique and immutable data type and may be used as an identifier for object properties. The symbol object is an implicit object wrapper for the symbol primitive data type. - [Mozilla Developer docs for Symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) 18 | 19 | ## Usage 20 | 21 | ```js 22 | const assignSymbols = require('assign-symbols'); 23 | let target = {}; 24 | 25 | // add a symbol to object "one" 26 | let one = {}; 27 | let symbolOne = Symbol('aaa'); 28 | one[symbolOne] = 'bbb'; 29 | 30 | // add a symbol to object "two" 31 | let two = {}; 32 | let symbolTwo = Symbol('ccc'); 33 | two[symbolTwo] = 'ddd'; 34 | 35 | // assign symbols from objects "one" and "two" to object "target" 36 | assignSymbols(target, one, two); 37 | 38 | console.log(target[symbolOne]); //=> 'bbb' 39 | console.log(target[symbolTwo]); //=> 'ddd' 40 | ``` 41 | 42 | ## About 43 | 44 |
45 | Contributing 46 | 47 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 48 | 49 |
50 | 51 |
52 | Running Tests 53 | 54 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 55 | 56 | ```sh 57 | $ npm install && npm test 58 | ``` 59 | 60 |
61 | 62 |
63 | Building docs 64 | 65 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 66 | 67 | To generate the readme, run the following command: 68 | 69 | ```sh 70 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 71 | ``` 72 | 73 |
74 | 75 | ### Related projects 76 | 77 | You might also be interested in these projects: 78 | 79 | * [assign-deep](https://www.npmjs.com/package/assign-deep): Deeply assign the enumerable properties and/or es6 Symbol properies of source objects to the target… [more](https://github.com/jonschlinkert/assign-deep) | [homepage](https://github.com/jonschlinkert/assign-deep "Deeply assign the enumerable properties and/or es6 Symbol properies of source objects to the target (first) object.") 80 | * [clone-deep](https://www.npmjs.com/package/clone-deep): Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives. | [homepage](https://github.com/jonschlinkert/clone-deep "Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives.") 81 | * [extend-shallow](https://www.npmjs.com/package/extend-shallow): Extend an object with the properties of additional objects. node.js/javascript util. | [homepage](https://github.com/jonschlinkert/extend-shallow "Extend an object with the properties of additional objects. node.js/javascript util.") 82 | * [merge-deep](https://www.npmjs.com/package/merge-deep): Recursively merge values in a javascript object. | [homepage](https://github.com/jonschlinkert/merge-deep "Recursively merge values in a javascript object.") 83 | * [mixin-deep](https://www.npmjs.com/package/mixin-deep): Deeply mix the properties of objects into the first object. Like merge-deep, but doesn't clone… [more](https://github.com/jonschlinkert/mixin-deep) | [homepage](https://github.com/jonschlinkert/mixin-deep "Deeply mix the properties of objects into the first object. Like merge-deep, but doesn't clone. No dependencies.") 84 | 85 | ### Contributors 86 | 87 | | **Commits** | **Contributor** | 88 | | --- | --- | 89 | | 4 | [jonschlinkert](https://github.com/jonschlinkert) | 90 | | 2 | [phated](https://github.com/phated) | 91 | 92 | ### Author 93 | 94 | **Jon Schlinkert** 95 | 96 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) 97 | * [GitHub Profile](https://github.com/jonschlinkert) 98 | * [Twitter Profile](https://twitter.com/jonschlinkert) 99 | 100 | ### License 101 | 102 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). 103 | Released under the [MIT License](LICENSE). 104 | 105 | *** 106 | 107 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on August 06, 2018._ -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | if (typeof Symbol === 'function') { 4 | const assignSymbols = require('./'); 5 | let obj = {}; 6 | 7 | let one = {}; 8 | let symbolOne = Symbol('aaa'); 9 | one[symbolOne] = 'bbb'; 10 | 11 | let two = {}; 12 | let symbolTwo = Symbol('ccc'); 13 | two[symbolTwo] = 'ddd'; 14 | 15 | assignSymbols(obj, one, two); 16 | console.log(obj[symbolOne]); //=> 'bbb' 17 | console.log(obj[symbolTwo]); //=> 'ddd' 18 | } else { 19 | console.log('Sorry, Symbol is not supported on ' + process.version + ' of node.js'); 20 | } 21 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * assign-symbols 3 | * 4 | * Copyright (c) 2015-present, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | const toString = Object.prototype.toString; 11 | const isEnumerable = Object.prototype.propertyIsEnumerable; 12 | const getSymbols = Object.getOwnPropertySymbols; 13 | 14 | module.exports = (target, ...args) => { 15 | if (!isObject(target)) { 16 | throw new TypeError('expected the first argument to be an object'); 17 | } 18 | 19 | if (args.length === 0 || typeof Symbol !== 'function' || typeof getSymbols !== 'function') { 20 | return target; 21 | } 22 | 23 | for (let arg of args) { 24 | let names = getSymbols(arg); 25 | 26 | for (let key of names) { 27 | if (isEnumerable.call(arg, key)) { 28 | target[key] = arg[key]; 29 | } 30 | } 31 | } 32 | return target; 33 | }; 34 | 35 | function isObject(val) { 36 | return typeof val === 'function' || toString.call(val) === '[object Object]' || Array.isArray(val); 37 | } 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "assign-symbols", 3 | "description": "Assign the enumerable es6 Symbol properties from one or more objects to the first object passed on the arguments. Can be used as a supplement to other extend, assign or merge methods as a polyfill for the Symbols part of the es6 Object.assign method.", 4 | "version": "2.0.2", 5 | "homepage": "https://github.com/jonschlinkert/assign-symbols", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/assign-symbols", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/assign-symbols/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=6" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "devDependencies": { 23 | "gulp-format-md": "^1.0.0", 24 | "mocha": "^5.2.0" 25 | }, 26 | "keywords": [ 27 | "assign", 28 | "es6", 29 | "merge", 30 | "mixin", 31 | "polyfill", 32 | "primitive", 33 | "symbol", 34 | "symbols" 35 | ], 36 | "verb": { 37 | "toc": false, 38 | "layout": "default", 39 | "tasks": [ 40 | "readme" 41 | ], 42 | "plugins": [ 43 | "gulp-format-md" 44 | ], 45 | "related": { 46 | "list": [ 47 | "assign-deep", 48 | "clone-deep", 49 | "extend-shallow", 50 | "merge-deep", 51 | "mixin-deep" 52 | ] 53 | }, 54 | "lint": { 55 | "reflinks": true 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | const assert = require('assert'); 5 | const assign = require('./'); 6 | 7 | describe('assign symbols', () => { 8 | it('should throw an error when invalid arguments are passed', () => { 9 | assert.throws(() => assign(), /expected the first argument to be an object/); 10 | }); 11 | 12 | it('should return the first object when no other args are passed', () => { 13 | let foo = { a: 'b' }; 14 | assert(assign(foo) === foo); 15 | }); 16 | 17 | it('should assign symbol properties from an object to the receiver', () => { 18 | let a = {}; 19 | let b = {}; 20 | let key = Symbol('abc'); 21 | b[key] = 'xyz'; 22 | assign(a, b); 23 | assert.equal(a[key], 'xyz'); 24 | }); 25 | 26 | it('should assign symbol properties from each object to the receiver', () => { 27 | let target = {}; 28 | let a = {}; 29 | let aa = Symbol('aa'); 30 | a[aa] = 'aa'; 31 | 32 | let b = {}; 33 | let bb = Symbol('bb'); 34 | b[bb] = 'bb'; 35 | 36 | let c = {}; 37 | let cc = Symbol('cc'); 38 | c[cc] = 'cc'; 39 | 40 | assign(target, a, b, c); 41 | assert.equal(target[aa], 'aa'); 42 | assert.equal(target[bb], 'bb'); 43 | assert.equal(target[cc], 'cc'); 44 | }); 45 | 46 | it('should not assign non-enumerable symbols', () => { 47 | let a = {}; 48 | let key = Symbol('abc'); 49 | function App() {} 50 | App.prototype[key] = 'xyz'; 51 | let app = new App(); 52 | assign(a, app); 53 | assert.equal(typeof a[key], 'undefined'); 54 | }); 55 | 56 | it('should return the receiver object', () => { 57 | let a = {}; 58 | let b = {}; 59 | let key = Symbol('abc'); 60 | b[key] = 'xyz'; 61 | let res = assign(a, b); 62 | assert.equal(res[key], 'xyz'); 63 | }); 64 | }); 65 | --------------------------------------------------------------------------------