[js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展camelize与dasherize方法(3)
在此之前,我们已经完成了4个方法:
trimLeft, trimRight, trim, capitalize
本文,我们扩展驼峰式与下划线转化这两个对称的方法
camelize: 把空格,下划线,中横线后面的首字母大写.
dasherize: 把空格后面的大写字母,大写字母,下划线后面的大写字母,变成 中横线 + 对应的小写字母:
如: MozBorderRadius 变成 -moz-border-radius
; (function (window, undefined) {
function init(obj, s) {
if (s !== null && s !== undefined) {
if (typeof s === 'string') {
obj.s = s;
} else {
obj.s = s.toString();
}
} else {
obj.s = s;
}
} function G(s) {
init(this, s);
} function GhostWu(s) {
return new G(s);
} var sProto = String.prototype;
G.prototype = {
constructor: G,
capitalize: function () {
return new this.constructor(this.s.slice(0, 1).toUpperCase() + this.s.substring(1).toLowerCase());
},
trimLeft: function () {
var s;
if (sProto.trimLeft === 'undefined')
s = this.s.trimLeft();
else
s = this.s.replace(/^\s+/g, '');
return new this.constructor(s);
},
trimRight: function () {
var s;
if (sProto.trimRight === 'undefined')
s = this.s.trimRight();
else
s = this.s.replace(/\s+$/g, '');
return new this.constructor(s);
},
trim: function () {
var s;
if (typeof sProto.trim === 'undefined') {
s = this.s.replace(/^\s+|\s+$/g, '');
} else {
s = this.s.trim();
}
return new this.constructor(s);
},
camelize : function () {
var s = this.trim().s.replace(/(\-|_|\s)+(.)?/g, function ( s0, s1, s2 ) {
return ( s2 ? s2.toUpperCase() : '' );
});
return new this.constructor(s);
},
dasherize : function(){
var s = this.trim().s.replace(/[_\s]+/g, '-').replace(/([A-Z])/g, '-$1').replace(/-+/g, '-').toLowerCase();
return new this.constructor(s);
}
}; window.G = GhostWu;
})(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
[js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展camelize与dasherize方法(3)的更多相关文章
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展trim,trimLeft,trimRight方法(2)
我们接着上一篇的继续,在上一篇我们完成了工具库的架构,本文扩展字符串去空格的方法, 一共有3个 1,trimLeft: 去除字符串左边的空格 2,trimRight: 去除字符串右边的空格 3,tri ...
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展字符串位置方法(4)
本文,我们接着之前的框架继续扩展,这次扩展了一共有5个与字符串位置相关的方法 between( left, right ) 返回两个字符串之间的内容, 如果第二个参数没有传递,返回的是找到的第一个参数 ...
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)
所谓字符串工具库就是利用javascript面向对象的知识封装一个常用的字符串处理方法库,首先给这个库起个名字,好吧就叫ghostwu.js. 看下ghostwu.js的整体架构: ; (functi ...
- [js高手之路]设计模式系列课程-设计一个模块化扩展功能(define)和使用(use)库
模块化的诞生标志着javascript开发进入工业时代,近几年随着es6, require js( sea js ), node js崛起,特别是es6和node js自带模块加载功能,给大型程序开发 ...
- [js高手之路] html5 canvas教程 - 制作一个数码倒计时效果
效果图: 这个实例主要注意: 1,剩余时间的计算 2,每个时间数字的绘制 时间主要有0-9和一个冒号组成,用数组来表示( 0: 就是不画圆,1:就是画一个蓝色的圆 ) num.js文件: var di ...
- [js高手之路]封装运动框架实战左右与上下滑动的焦点轮播图
在这篇文章[js高手之路]打造通用的匀速运动框架中,封装了一个匀速运动框架,我们在这个框架的基础之上,加上缓冲运动效果,然后用运动框架来做幻灯片(上下,左右),效果如下: 1 2 3 4 5 // 0 ...
- [js高手之路]原型对象(prototype)与原型链相关属性与方法详解
一,instanceof: instanceof检测左侧的__proto__原型链上,是否存在右侧的prototype原型. 我在之前的两篇文章 [js高手之路]构造函数的基本特性与优缺点 [js高手 ...
- [js高手之路]设计模式系列课程-组合模式+寄生组合继承实战新闻列表
所谓组合模式,就是把一堆结构分解出来,组成在一起,现实中很多这样的例子,如: 1.肯德基套餐就是一种组合模式, 比如鸡腿堡套餐,一般是是由一个鸡腿堡,一包薯条,一杯可乐等组成的 2.组装的台式机同理, ...
- [js高手之路]Node.js实现简易的爬虫-抓取博客文章列表信息
抓取目标:就是我自己的博客:http://www.cnblogs.com/ghostwu/ 需要实现的功能: 抓取文章标题,超链接,文章摘要,发布时间 需要用到的库: node.js自带的http库 ...
随机推荐
- c# 上传附件大小限制的问题
在c# 相关的asp.net 中.需要设置附件的大小.需要修改2部分. 1.修改metabase.XML 以Windows2003 为例子. 打开 C:\Windows\System32\Inets ...
- ELK日志框架(2):log4net.ElasticSearch+ Kibana实现日志记录和显示
环境说明 1. windows server 2012 R2 64位 2. log4net.ElasticSearch 3. kibana-5.5.0-windows-x86.zip 架构说明 数据采 ...
- dfs.datanode.max.transfer.threads
An HDFS DataNode has an upper bound on the number of files that it will serve at any one time: <p ...
- Sql 2008R2 windows身份好用 ,sa身份不好用
Sql server2008r2 安装完毕以后 windows身份验证好用,sa身份不好用,解决方法步骤如下: 1.首先用windows身份登录 2.SQL实例右键属性 3.安全性这一项 4.选择wi ...
- TP3.2写提交的验证码验证
把今天掌握的东西整理一下,要不然,我就忘干净了: 今天在做一个企业网站的时候,有一个在线留言的功能,最后提交的时候需要输入验证码.如图下: 当然,特连接的并不是我的后台 好了,开始了,首先我需要把验证 ...
- 把int型非负数转换为英文
数字转换为英文 输入为int型非负数,最大值为2^31 - 1 = 2 147 483 647 输出为String英文,最大输出为Two Billion One Hundred Forty Seven ...
- 安装Scala-2.11.7——集群学习日记
前言 在安装Spark之前,我们需要安装Scala语言的支持.在此我选择的是scala-2.11.7版本. scala-2.11.7下载 为了方便,我现在我的SparkMaster主机上先安装,把目录 ...
- 使用Dubbo、JSF等RPC框架时,对于异常的处理
无论是Dubbo还是JSF等RPC框架,一般都会把接口分为2部分: 1,服务端(provider) 2,客户端(consumer) 由于,客户端与服务端可能不在同一个应用中,所以客户端一般在调用服务端 ...
- (转)SQL Server基础之存储过程(清晰使用)
阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储过程 简单来说,存储过程就是一条或 ...
- css3-d ,动画,圆角
一.3D 开启元素3D transform-style: preserve-3d; Z轴 正数 屏幕外,反之屏幕内 近大远小 perspective: length (必须大于等于0) -- 在3D元 ...