Skip to content

Commit

Permalink
create
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Jul 23, 2016
0 parents commit cb4b127
Show file tree
Hide file tree
Showing 12 changed files with 431 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# editorconfig.org
root = true

# Unix-style newlines with a newline ending every file
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{html,js,ts,css,scss,less,xml,json,es6}]
indent_style = space
indent_size = 2

[*.yml]
indent_style = space
indent_size = 2

[*output*]
trim_trailing_whitespace = false
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Logs
logs
*.log

# vim
*.sw[a-z]
vim/.netrwhist

# temp
.DS_Store
*.db
*.bak
*.tmp
*.cmd
~*
*.idea
._*

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules

# Bower
coverage
bower_components

# test
assets

# release
release
example/debug.html
5 changes: 5 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"undef": false,
"esversion": 6,
"evil": true
}
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- 4.4.7
script:
- npm run lint
- npm run test
after_script:
- "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
branches:
only:
- master
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
termstring 条件字符串处理
-------

# [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coverage-image]][coverage-url]

## 背景

生成比较直观的键值字符串用。

## 示例

### 转换前

```js
termstring.stringify(
{
"a": 1,
"b": 2,
"c": 3,
}
);
```

### 转换后

```js
"[a]1[b]2[c]3"
```

## License

MIT © [zswang](http://weibo.com/zswang)

[npm-url]: https://npmjs.org/package/termstring
[npm-image]: https://badge.fury.io/js/termstring.svg
[travis-url]: https://travis-ci.org/zswang/termstring
[travis-image]: https://travis-ci.org/zswang/termstring.svg?branch=master
[coverage-url]: https://coveralls.io/github/zswang/termstring?branch=master
[coverage-image]: https://coveralls.io/repos/zswang/termstring/badge.svg?branch=master&service=github
31 changes: 31 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*jshint globalstrict: true*/
/*global require*/

'use strict';

var gulp = require('gulp');
var jdists = require('gulp-jdists');
var examplejs = require('gulp-examplejs');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');

gulp.task('build', function() {
return gulp.src(['src/termstring.js'])
.pipe(jdists({
trigger: 'release'
}))
.pipe(gulp.dest('./'))
.pipe(uglify())
.pipe(rename('termstring.min.js'))
.pipe(gulp.dest('./'));
});

gulp.task('example', function() {
return gulp.src('src/**.js')
.pipe(examplejs({
header: "var termstring = require('../');\n"
}))
.pipe(gulp.dest('test'));
});

gulp.task('default', ['build']);
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "termstring",
"version": "0.0.1",
"description": "The termstring module provides utilities for parsing and formatting term strings.",
"main": "termstring.js",
"repository": {
"type": "git",
"url": "https://github.com/zswang/termstring.git"
},
"keywords": [
"term",
"parse"
],
"author": {
"name": "zswang",
"url": "http://weibo.com/zswang"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/zswang/termstring/issues"
},
"homepage": "https://github.com/zswang/termstring",
"devDependencies": {
"gulp": "^3.9.0",
"gulp-examplejs": "^0.0.9",
"gulp-jdists": "^0.0.5",
"gulp-uglify": "^1.5.3",
"gulp-rename": "^1.2.2",
"istanbul": "^0.3.17",
"jshint": "^2.5.8",
"mocha": "^2.0.1",
"should": "^4.1.0"
},
"dependencies": {},
"scripts": {
"_update_version": "node version.js",
"_dist": "gulp",
"lint": "jshint src/*.js",
"test": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -R spec",
"example": "gulp example",
"mocha": "mocha",
"dist": "npm run _update_version && npm run example && npm run _dist && npm run test"
},
"files": [
"lib",
"README.md"
]
}
99 changes: 99 additions & 0 deletions src/termstring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
(function(exportName) {

/*<remove>*/
'use strict';
/*</remove>*/

var exports = {};

/*<jdists encoding="ejs" data="../package.json">*/
/**
* @file <%- name %>
*
* <%- description %>
* @author
<% (author instanceof Array ? author : [author]).forEach(function (item) { %>
* <%- item.name %> (<%- item.url %>)
<% }); %>
* @version <%- version %>
<% var now = new Date() %>
* @date <%- [
now.getFullYear(),
now.getMonth() + 101,
now.getDate() + 100
].join('-').replace(/-1/g, '-') %>
*/
/*</jdists>*/

/*<function name="termstring_stringify">*/
/**
* 条件数据格式化
*
* @param {Object} data 条件数据
* @return {String} 返回格式化字符串
* @example stringify():base
```js
var data = {a: 1, b: 2, c: 3};
console.log(termstring.stringify(data));
// > [a]1[b]2[c]3
```
* @example stringify():boundary
```js
var data = {a:"", b: 2, c:""};
console.log(termstring.stringify(data));
// > [a][b]2[c]
```
*/
function termstring_stringify(data) {
var result = '';
Object.keys(data).forEach(function(key) {
result += '[' + key + ']' + data[key];
});
return result;
}
/*</function>*/
exports.stringify = termstring_stringify;

/*<function name="termstring_parse">*/
/**
* 解析条件数据
*
* @param {String} text 条件格式化字符串
* @return {Object} 返回条件数据
* @example parse():base
```js
console.log(JSON.stringify(termstring.parse('[a]1[b]2[c]3')));
// > {"a":"1","b":"2","c":"3"}
```
* @example parse():boundary
```js
console.log(JSON.stringify(termstring.parse('[a][b]2[c]')));
// > {"a":"","b":"2","c":""}
```
*/
function termstring_parse(text) {
var result = {};
String(text).replace(/\[(.*?)\]([^\[\]]*)/g, function(all, key, value) {
result[key] = value;
});
return result;
}
/*</function>*/
exports.parse = termstring_parse;

/* istanbul ignore next */
if (typeof define === 'function') {
if (define.amd) {
define(function () {
return exports;
});
}
}
else if (typeof module !== 'undefined' && module.exports) {
module.exports = exports;
}
else {
window[exportName] = exports;
}

})('termstring');
80 changes: 80 additions & 0 deletions termstring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
(function(exportName) {
var exports = {};
/**
* @file termstring
*
* The termstring module provides utilities for parsing and formatting term strings.
* @author
* zswang (http://weibo.com/zswang)
* @version 0.0.1
* @date 2016-07-23
*/
/*<function name="termstring_stringify">*/
/**
* 条件数据格式化
*
* @param {Object} data 条件数据
* @return {String} 返回格式化字符串
* @example stringify():base
```js
var data = {a: 1, b: 2, c: 3};
console.log(termstring.stringify(data));
// > [a]1[b]2[c]3
```
* @example stringify():boundary
```js
var data = {a:"", b: 2, c:""};
console.log(termstring.stringify(data));
// > [a][b]2[c]
```
*/
function termstring_stringify(data) {
var result = '';
Object.keys(data).forEach(function(key) {
result += '[' + key + ']' + data[key];
});
return result;
}
/*</function>*/
exports.stringify = termstring_stringify;
/*<function name="termstring_parse">*/
/**
* 解析条件数据
*
* @param {String} text 条件格式化字符串
* @return {Object} 返回条件数据
* @example parse():base
```js
console.log(JSON.stringify(termstring.parse('[a]1[b]2[c]3')));
// > {"a":"1","b":"2","c":"3"}
```
* @example parse():boundary
```js
console.log(JSON.stringify(termstring.parse('[a][b]2[c]')));
// > {"a":"","b":"2","c":""}
```
*/
function termstring_parse(text) {
var result = {};
String(text).replace(/\[(.*?)\]([^\[\]]*)/g, function(all, key, value) {
result[key] = value;
});
return result;
}
/*</function>*/
exports.parse = termstring_parse;
/* istanbul ignore next */
if (typeof define === 'function') {
if (define.amd) {
define(function () {
return exports;
});
}
}
else if (typeof module !== 'undefined' && module.exports) {
module.exports = exports;
}
else {
window[exportName] = exports;
}
})('termstring');
1 change: 1 addition & 0 deletions termstring.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cb4b127

Please sign in to comment.