[js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展camelize与dasherize方法(3)
来源:互联网 发布:网络侵权管辖 编辑:程序博客网 时间:2024/06/06 17:43
在此之前,我们已经完成了4个方法:
trimLeft, trimRight, trim, capitalize
本文,我们扩展驼峰式与下划线转化这两个对称的方法
camelize: 把空格,下划线,中横线后面的首字母大写.
dasherize: 把空格后面的大写字母,大写字母,下划线后面的大写字母,变成 中横线 + 对应的小写字母:
如: MozBorderRadius 变成 -moz-border-radius
1 ; (function (window, undefined) { 2 function init(obj, s) { 3 if (s !== null && s !== undefined) { 4 if (typeof s === 'string') { 5 obj.s = s; 6 } else { 7 obj.s = s.toString(); 8 } 9 } else {10 obj.s = s;11 }12 }13 14 function G(s) {15 init(this, s);16 }17 18 function GhostWu(s) {19 return new G(s);20 }21 22 var sProto = String.prototype;23 G.prototype = {24 constructor: G,25 capitalize: function () {26 return new this.constructor(this.s.slice(0, 1).toUpperCase() + this.s.substring(1).toLowerCase());27 },28 trimLeft: function () {29 var s;30 if (sProto.trimLeft === 'undefined')31 s = this.s.trimLeft();32 else33 s = this.s.replace(/^\s+/g, '');34 return new this.constructor(s);35 },36 trimRight: function () {37 var s;38 if (sProto.trimRight === 'undefined')39 s = this.s.trimRight();40 else41 s = this.s.replace(/\s+$/g, '');42 return new this.constructor(s);43 },44 trim: function () {45 var s;46 if (typeof sProto.trim === 'undefined') {47 s = this.s.replace(/^\s+|\s+$/g, '');48 } else {49 s = this.s.trim();50 }51 return new this.constructor(s);52 },53 camelize : function () {54 var s = this.trim().s.replace(/(\-|_|\s)+(.)?/g, function ( s0, s1, s2 ) {55 return ( s2 ? s2.toUpperCase() : '' );56 });57 return new this.constructor(s);58 },59 dasherize : function(){60 var s = this.trim().s.replace(/[_\s]+/g, '-').replace(/([A-Z])/g, '-$1').replace(/-+/g, '-').toLowerCase();61 return new this.constructor(s);62 }63 };64 65 window.G = GhostWu;66 })(window, undefined);
console.log( G( 'ghost wu tell you how to learn js').camelize().s );//ghostWuTellYouHowToLearnJsconsole.log( G( 'ghost-wu-tell you how to learn js').camelize().s );//ghostWuTellYouHowToLearnJsconsole.log( G( 'ghost_wu_tell you how to learn js').camelize().s );//ghostWuTellYouHowToLearnJsconsole.log( G( '-moz-border-radius' ).camelize().s );//MozBorderRadiusconsole.log( G( 'MozBorderRadius' ).dasherize().s ); //-moz-border-radiusconsole.log( G( 'backgroundColor' ).dasherize().s ); //background-colorconsole.log( G( 'background color' ).dasherize().s ); //background-color
阅读全文
0 0
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展camelize与dasherize方法(3)
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)
- 每天学习API之二 ,zepto源码camelize, dasherize
- 跟我一起学Python之九:字符串常用方法
- [js高手之路]设计模式系列课程-设计一个模块化扩展功能(define)和使用(use)库
- [js高手之路]设计模式系列课程-设计一个模块化扩展功能(define)和使用(use)库
- [js高手之路]设计模式系列课程-设计一个模块化扩展功能(define)和使用(use)库
- JS字符串方法扩展
- JS字符串方法扩展
- js封装一个出去字符串中的空格方法
- js中字符串扩展方法
- JS数组字符串方法扩展
- 跟我一起学Python之四:字符串简介
- 跟我一起学Windows界面封装(五) 之 封装第一个控件:按钮
- 跟我一起学Python之十一:字典方法
- 一位director高手的成长之路,与大家一起共勉!
- 封装一个常用的js工具类
- wireshark插件开发
- 判断字节序和操作系统
- 放苹果
- Yii 框架学习--03 多应用多模块
- Python中 copy, deepcopy 的区别
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展camelize与dasherize方法(3)
- 怎样打开fis的文件
- python列表list的特殊操作
- 持续集成和Jenkins介绍
- 秒杀抢购开发总结
- 微信 Android 视频编码爬过的那些坑
- python之zip压缩函数
- Android开发 之 WebView的使用
- Java中关于类加载器的一些事儿(一)