Gone With the Wind

coffee在编写过程中的一些坑

CoffeeScript在编写过程之中,相对于JS可以带来很多便利性,但是同时也会引入一些问题,JS相对于Coffee来说有一些繁琐,但是相对严谨。

这里会列出coffee诸多的坑,其实也不能算是坑,只是在还没有灵活运用时,很难排查的一些小问题,也要注意一下自己平时的编写习惯。

  • 在生产过程中的一个案例。
1
2
3
4
5
6
7
{
mode,
base_cdn,
# data : encodeURIComponent JSON.stringify { formData : body, type },
web_config: JSON.stringify web_config,
user : locals.user
}

这个JSON原来的代码是:

1
2
3
4
5
6
{
mode,
base_cdn,
# data : encodeURIComponent JSON.stringify { formData : body, type },
web_config: JSON.stringify web_config,
}

因为Coffee在描述JSON时候无需添加逗号,然后写这段代码的同事恰好将每一行后面都加上了逗号,然后我一看,没啥问题,紧接着加了一个属性”user”,乍一看,基于coffee松散的语法,毫无任何问题。然后,发现后面,这个JSON的user属性始终是undefined,因为后面有很长一段逻辑,一直以为是后面的逻辑出了问题,经过一个下午的调试后,才开始怀疑是JSON的问题,编译一看,顿时内牛满面:

1
2
3
4
5
6
7
{
mode: mode,
base_cdn: base_cdn,
web_config: JSON.stringify(web_config, {
user: locals.user
}
}

node cover

nodejs 的三大特色:

nodejs

异步解决方案历史变迁

回调嵌套 -> event-pipe, async -> co -> fibjs

koa

基于co, 使用generator来使得异步代码使用起来同步化。

需求来了

  1. koa封装了大量逻辑, 只想使用类似connect的提供原始IncomingMessageServerResponse对象.

  2. 很多中间件都是采用connect中间件方式写的, 如果切换到koa, 必定要对原先的代码做修改.

node-cover

  1. 只提供基础的http server服务.

  2. 兼容connect中间件, 不兼容koa中间件, 但是可以使用ES6写的各种模块.

  3. 无缝切换现有项目, 同时使用co写同步代码.

使用方法

coffee-script v1.8

一. coffee-script的github master分支已经是1.8版本, 对es6很多语法都做了支持, 其中就包括generator.

1
2
3
4
5
6
7
# Function
( test ) ->
....

# GeneratorFuntion
( test ) ->
yield ->

二. 使用git地址安装npm包

package.json

1
2
3
4
5
6
7
8
9
10
11
12
{
.......
dependencies : {
"coffee-script" : "git+ssh://git@github.com:jashkenas/coffeescript.git"
}
}

//git://github.com/user/project.git#commit-ish
//git+ssh://user@hostname:project.git#commit-ish
//git+ssh://user@hostname/project.git#commit-ish
//git+http://user@hostname/project/blah.git#commit-ish
//git+https://user@hostname/project/blah.git#commit-ish

三. 同时, 1.8的coffee修正了一些逻辑, 偷懒的同学注意了

1
( @server ) ->

对于如上代码,1.8 版本以前的coffee会编译成

1
2
3
4
  fucntion( server ){
@server = server
.....
}

但是对于1.8版本coffee会编译成:

1
2
3
4
function( _at_server ){
@server = _at_server
.....
}

如果直接在函数中使用server变量的话,1.8版本之前是可以的,但是在1.8版本之后就会报错了。

如何使用hexo搭建一个个人博客

如何快速搭建一个hexo博客。

一. 安装hexo

1
npm install hexo -g

二. 初始化hexo目录

1
2
3
4
mkdir hexo
cd hexo
hexo init
npm install

三. 所有的博客文章都在source->_posts文件夹下面, 一个markdown文件就是一篇博客, 执行一下命令, 就能够在本地起一个服务预览一下。

1
2
3
4
# 将markdown文件编译成html
hexo generate
# 启动服务
hexo server

然后访问: http://127.0.0.1:4000, 就可以查看博客预览了。

四. 注册一个github账号, 创建一个一下名字的项目:#{github name}.github.io, 例如perterpon.github.io.

五. 修改hexo配置文件, 配置文件位于/_config.yml

e.g

1
2
3
4
5
6
7
deploy: 
# type 选择github
type: github
# 填写github地址
repository: https://github.com/zippera/zippera.github.io.git
# 选择相应分支, 使用github pages的时候一般默认是使用gh-pages分支作为使用的分支, 可以在github上设置.
branch: master

六. 创建github pages, 在之前创建好perterpon.github.io项目中, 点击settings- Automatic page generator按钮, 跟着提示一路确定就能完成创建了。

七. 在hexo文件夹中, 执行hexo deploy, 提示成功后等待几分钟访问http://perterpon.github.io, 就能访问自己的博客了。

八. 大量主题样式等等, 评论系统: 多说.

OSX 中获取UID和MAC地址的方法

很多时候需要搜集用户的MAC地址或者UID来进行用户的身份确认, UID和MAC地址都可以保持唯一性.

  • 获取UID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
+ ( NSString *) getHardwareUUID {
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/sbin/ioreg"];

//ioreg -rd1 -c IOPlatformExpertDevice | grep -E '(UUID)'

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-rd1", @"-c",@"IOPlatformExpertDevice",nil];
[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

NSString *key = @"IOPlatformUUID";
NSRange range = [string rangeOfString:key];

NSInteger location = range.location + [key length] + 5;
NSInteger length = 32 + 4;
range.location = location;
range.length = length;

NSString *UUID = [string substringWithRange:range];

UUID = [UUID stringByReplacingOccurrencesOfString:@"-" withString:@""];

return UUID;
}
  • 获取MAC地址
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
+ ( NSMutableString *) getMacAddr {
kern_return_t kr;
CFMutableDictionaryRef matchDict;
io_iterator_t iterator;
io_registry_entry_t entry;

matchDict = IOServiceMatching("IOEthernetInterface");
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchDict, &iterator);

NSDictionary *resultInfo = nil;

NSMutableString *macAddrs = [[ NSMutableString alloc ] init];

while ((entry = IOIteratorNext(iterator)) != 0)
{
CFMutableDictionaryRef properties=NULL;
kr = IORegistryEntryCreateCFProperties(entry,
&properties,
kCFAllocatorDefault,
kNilOptions);
if (properties)
{
resultInfo = (__bridge_transfer NSDictionary *)properties;
NSString *bsdName = [resultInfo objectForKey:@"BSD Name"];
NSData *macData = [resultInfo objectForKey:@"IOMACAddress"];
if (!macData)
{
continue;
}

NSMutableString *macAddress = [[NSMutableString alloc] init];
const UInt8 *bytes = [macData bytes];
for (int i=0; i<macData.length; i++)
{
[macAddress appendFormat:@"%02x",*(bytes+i)];
}
if (bsdName && macAddress)
{
[ macAddrs appendFormat:@"%@,", macAddress ];
}
}
}
IOObjectRelease(iterator);
return macAddrs;
}