├── .gitignore ├── .istanbul.yml ├── .editorconfig ├── .travis.yml ├── index.js ├── package.json ├── LICENSE ├── test └── test.coffee └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.sw[a-p] 2 | /node_modules/ 3 | npm-debug.log 4 | /coverage/ 5 | -------------------------------------------------------------------------------- /.istanbul.yml: -------------------------------------------------------------------------------- 1 | instrumentation: 2 | excludes: 3 | - test.js 4 | - test/**/* 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | script: 4 | - node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- --compilers coffee:coffee-script/register 5 | - cat coverage/lcov.info | node_modules/.bin/coveralls 6 | node_js: 7 | - 7 8 | - 6 9 | - 5 10 | - 4 11 | - 3 12 | - "0.10" 13 | - "0.11" 14 | - "0.12" 15 | - "iojs" 16 | os: 17 | - linux 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var isArrayish = require('is-arrayish'); 4 | 5 | var concat = Array.prototype.concat; 6 | var slice = Array.prototype.slice; 7 | 8 | var swizzle = module.exports = function swizzle(args) { 9 | var results = []; 10 | 11 | for (var i = 0, len = args.length; i < len; i++) { 12 | var arg = args[i]; 13 | 14 | if (isArrayish(arg)) { 15 | // http://jsperf.com/javascript-array-concat-vs-push/98 16 | results = concat.call(results, slice.call(arg)); 17 | } else { 18 | results.push(arg); 19 | } 20 | } 21 | 22 | return results; 23 | }; 24 | 25 | swizzle.wrap = function (fn) { 26 | return function () { 27 | return fn(swizzle(arguments)); 28 | }; 29 | }; 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simple-swizzle", 3 | "description": "Simply swizzle your arguments", 4 | "version": "0.2.4", 5 | "author": "Qix (http://github.com/qix-)", 6 | "keywords": [ 7 | "argument", 8 | "arguments", 9 | "swizzle", 10 | "swizzling", 11 | "parameter", 12 | "parameters", 13 | "mixed", 14 | "array" 15 | ], 16 | "license": "MIT", 17 | "scripts": { 18 | "pretest": "xo", 19 | "test": "mocha --compilers coffee:coffee-script/register" 20 | }, 21 | "files": [ 22 | "index.js" 23 | ], 24 | "repository": "qix-/node-simple-swizzle", 25 | "devDependencies": { 26 | "coffee-script": "^1.9.3", 27 | "coveralls": "^2.11.2", 28 | "istanbul": "^0.3.17", 29 | "mocha": "^2.2.5", 30 | "should": "^7.0.1", 31 | "xo": "^0.7.1" 32 | }, 33 | "dependencies": { 34 | "is-arrayish": "^0.3.1" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Josh Junon 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 | -------------------------------------------------------------------------------- /test/test.coffee: -------------------------------------------------------------------------------- 1 | should = require 'should' 2 | swizzle = require '../' 3 | 4 | Error.stackTraceLimit = Infinity 5 | 6 | swizMyself = -> swizzle arguments 7 | 8 | it 'should swizzle single values', -> 9 | (swizMyself 1, 2, 3, 4).should.deepEqual [1, 2, 3, 4] 10 | (swizMyself 1, 2, 3, 4).should.not.deepEqual [1, 2, 4] 11 | swizMyself().should.deepEqual [] 12 | 13 | it 'should swizzle a single array', -> 14 | (swizMyself [1, 2, 3, 4]).should.deepEqual [1, 2, 3, 4] 15 | 16 | it 'should swizzle interleaved values/arrays', -> 17 | (swizMyself 1, 2, [3, 4]).should.deepEqual [1, 2, 3, 4] 18 | (swizMyself [1, 2], [3, 4]).should.deepEqual [1, 2, 3, 4] 19 | (swizMyself [1, 2, 3], 4).should.deepEqual [1, 2, 3, 4] 20 | (swizMyself [1], [2], [3], [4]).should.deepEqual [1, 2, 3, 4] 21 | 22 | it 'should swizzle pseudo-arrays', -> 23 | (swizMyself 1, 2, {length: 2, 0: 3, 1: 4}).should.deepEqual [1, 2, 3, 4] 24 | (swizMyself {length: 1, 0: 1}, 2, 3, {length: 1, 0: 4}).should.deepEqual [1, 2, 3, 4] 25 | 26 | it 'should correctly swizzle string arguments', -> 27 | (swizMyself 'hello').should.deepEqual ['hello'] 28 | (swizMyself ['hello']).should.deepEqual ['hello'] 29 | (swizMyself ['hello'], 'there').should.deepEqual ['hello', 'there'] 30 | 31 | it 'should wrap a function for swizzling', -> 32 | fn = (args) -> args 33 | swizzleMyFn = swizzle.wrap fn 34 | 35 | (swizzleMyFn 1, 2, 3, 4).should.deepEqual [1, 2, 3, 4] 36 | (swizzleMyFn 1, 2, [3, 4]).should.deepEqual [1, 2, 3, 4] 37 | (swizzleMyFn [1, 2], [3, 4]).should.deepEqual [1, 2, 3, 4] 38 | (swizzleMyFn [1, 2, 3, 4]).should.deepEqual [1, 2, 3, 4] 39 | (swizzleMyFn [1], [2], [3], [4]).should.deepEqual [1, 2, 3, 4] 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > **NOTE:** ⚠️ **Don't use this package in new projects.** It is a **huge anti-pattern** and will only confuse and annoy people who use whatever code you write with it. I wrote this in a time when Javascript and Node.js were still pretty experimental and clever things like this weren't frowned upon. I've also learned a LOT about proper API design since I wrote this package. DO. NOT. USE. THIS. PACKAGE. If you're reaching for it, please *really* reconsider your API's design. 2 | 3 | --- 4 | 5 | # simple-swizzle [![Travis-CI.org Build Status](https://img.shields.io/travis/Qix-/node-simple-swizzle.svg?style=flat-square)](https://travis-ci.org/Qix-/node-simple-swizzle) [![Coveralls.io Coverage Rating](https://img.shields.io/coveralls/Qix-/node-simple-swizzle.svg?style=flat-square)](https://coveralls.io/r/Qix-/node-simple-swizzle) 6 | 7 | > [Swizzle](https://en.wikipedia.org/wiki/Swizzling_(computer_graphics)) your function arguments; pass in mixed arrays/values and get a clean array 8 | 9 | ## Usage 10 | 11 | ```js 12 | var swizzle = require('simple-swizzle'); 13 | 14 | function myFunc() { 15 | var args = swizzle(arguments); 16 | // ... 17 | return args; 18 | } 19 | 20 | myFunc(1, [2, 3], 4); // [1, 2, 3, 4] 21 | myFunc(1, 2, 3, 4); // [1, 2, 3, 4] 22 | myFunc([1, 2, 3, 4]); // [1, 2, 3, 4] 23 | ``` 24 | 25 | Functions can also be wrapped to automatically swizzle arguments and be passed 26 | the resulting array. 27 | 28 | ```js 29 | var swizzle = require('simple-swizzle'); 30 | 31 | var swizzledFn = swizzle.wrap(function (args) { 32 | // ... 33 | return args; 34 | }); 35 | 36 | swizzledFn(1, [2, 3], 4); // [1, 2, 3, 4] 37 | swizzledFn(1, 2, 3, 4); // [1, 2, 3, 4] 38 | swizzledFn([1, 2, 3, 4]); // [1, 2, 3, 4] 39 | ``` 40 | 41 | ## License 42 | Licensed under the [MIT License](http://opensource.org/licenses/MIT). 43 | You can find a copy of it in [LICENSE](LICENSE). 44 | --------------------------------------------------------------------------------