├── readme.md ├── LICENSE └── index.js /readme.md: -------------------------------------------------------------------------------- 1 | ## underscore2camelCase 2 | > 下划线命名法和驼峰式命名法相互转化 3 | 4 | ##Usage 5 | ### 转换字符串 6 | ``` 7 | underscore2camelCase.camelize('hello_world') // 'helloWorld' 8 | underscore2camelCase.decamelize('fooBar') // 'foo_bar' 9 | underscore2camelCase.decamelize('fooBarBaz', { separator: '-' }) // 'foo-bar-baz' 10 | ``` 11 | ### 转换Object keys 12 | ``` 13 | var object = { attr_one: 'foo', attr_two: 'bar' } 14 | underscore2camelCase.camelizeKeys(object); // { attrOne: 'foo', attrTwo: 'bar' } 15 | ``` 16 | 17 | ``` 18 | var array = [{ attr_one: 'foo' }, { attr_one: 'bar' }] 19 | underscore2camelCase.camelizeKeys(array); // [{ attrOne: 'foo' }, { attrOne: 'bar' }] 20 | ``` 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 宝丁 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // anthor: wing 2 | 3 | ;(function(global) { 4 | 5 | var toString = Object.prototype.toString; 6 | var _isFunction = function(obj) { 7 | return typeof(obj) === 'function'; 8 | }; 9 | var _isObject = function(obj) { 10 | return obj === Object(obj); 11 | }; 12 | var _isArray = function(obj) { 13 | return toString.call(obj) === '[object Array]'; 14 | }; 15 | var _isDate = function(obj) { 16 | return toString.call(obj) === '[object Date]'; 17 | }; 18 | var _isRegExp = function(obj) { 19 | return toString.call(obj) === '[object RegExp]'; 20 | }; 21 | var _isBoolean = function(obj) { 22 | return toString.call(obj) === '[object Boolean]'; 23 | }; 24 | var _isNumberical = function(obj) { 25 | obj = obj - 0; 26 | return obj === obj; 27 | }; 28 | 29 | var camelize = function(string) { 30 | if(_isNumberical(string)) { 31 | return string; 32 | } 33 | string = string.replace(/[\-_\s]+(.)?/g, function(match, chr) { 34 | return chr ? chr.toUpperCase() : ''; 35 | }); 36 | return string.substr(0, 1).toLowerCase() + string.substr(1); 37 | } 38 | 39 | var pascalize = function(string) { 40 | var camelized = camelize(string); 41 | return camelized.substr(0, 1).toUpperCase() + camelized.substr(1); 42 | } 43 | 44 | var seperateWords = function(string, options) { 45 | options = options || {}; 46 | var separator = options.separator || '_'; 47 | var split = options.split || /(?=[A-Z])/; 48 | 49 | return string.split(split).join(separator); 50 | } 51 | 52 | var decamelize = function(string, options) { 53 | return seperateWords(string, options).toLowerCase(); 54 | } 55 | 56 | var _processor = function(convert, options) { 57 | var callback = options && 'process' in options ? options.process : options; 58 | 59 | if(typeof(callback) !== 'function') { 60 | return convert; 61 | } 62 | 63 | return function(string, options) { 64 | return callback(string, convert, options); 65 | } 66 | }; 67 | 68 | var _processKeys = function(convert, obj, options) { 69 | if(!_isObject(obj) || _isDate(obj) || _isRegExp(obj) || _isBoolean(obj) || _isFunction(obj)) { 70 | return obj; 71 | } 72 | 73 | var output, 74 | i = 0, 75 | l = 0; 76 | 77 | if(_isArray(obj)) { 78 | output = []; 79 | for(l=obj.length; i