yanjs
v3.1.0
Published
This is just a common toolkit for the Front End
Downloads
21
Readme
yanjs
This is a tiny common toolkit for the Front End
Installation
$ npm install yanjs
Example
var yanjs = require('yanjs');
API
快速使用
- base64转换
yan.toBase64("中文abc123"); // "JUU0JUI4JUFEJUU2JTk2JTg3YWJjMTIz"
yan.fromBase64("JUU0JUI4JUFEJUU2JTk2JTg3YWJjMTIz"); // "中文abc123"
- 格式转换
yan.toThousands(123456789); // 123,456,789
yan.toFormatDate("YYYY-MM-DD hh:ii:ss.mss WWW qq"); // "2018-01-08 03:50:39.102 星期一 01"
yan.toFormatDate("YYYY-MM-DD hh:ii:ss.mss WWWW qqqq",new Date(2018,00,02)); // "2018-01-02 12:00:00.000 星期二 01"
yan.toCamelCase('background-color'); // "backgroundColor"
yan.toCamelCase('-webkit-background-image'); // "WebkitBackgroundImage"
yan.toPascalCase('background-color'); // "BackgroundColor"
yan.toPascalCase('-webkit-background-image'); // "WebkitBackgroundImage"
yan.toUnicode("你好"); // '\u4f60\u597d'
yan.toByte('2k'); // 2048
yan.formatStr('My name is {0}', 'Yangfan'); // 'My name is Yangfan'
yan.formatStr("My name is {0}, and I'm {1} years old", ['Yangfan', 24]); // "My name is Yangfan, and I'm 24 years old"
- 数据序列化
yan.params.serialize({"name": "Yangfan","age": 24}); // 'name=Yangfan&age=24'
yan.params.serialize({"name":"Yangfan","fruits":["apple","banana"]}, true); // 'name=Yangfan&fruits=apple&fruits=banana'
yan.params.reSerialize('name=Yangfan&age=24'); // {"name": "Yangfan","age": "24"}
yan.params.reSerialize('name=Yangfan&fruits=apple&fruits=banana'); // {"name": "Yangfan","fruits": ["apple", "banana"]}
- Url解析
yan.urlParse("http://yanfan.com:83/home/disc/test.html#query?name=yan&age=100"); // {protocol: "http", username: "", password: "", host: "yanfan.com:83", port: "83", …}
- 获取、设置、删除cookie
// 判断是否存在此cookie
yan.docCookie.has("user_id");
// 设置
yan.docCookie.set("user_id", "12fdsa4f4a", {
expires:new Date(2018,12,31),
path:"/blog",
domain:".yangfan.com",
isSecure: true
});
// 获取
yan.docCookie.get("user_id");
// 删除
yan.docCookie.remove("user_id");
- Ajax
// ajax
yan.http2({
url:"yangfan.com/post",
method:"post",
data:{
name:"jobs",
age:100
},
success:function (data) {},
error:function (err) {}
});
// jsonp
yan.getJSON("yangfan.com/get",{
name:"jobs",
age:100
},success:function (data) {},
error:function (err) {});
- 浏览器
yan.isIE; // true | false
yan.browserDetail; // {name: "chrome", version: "69.0.3497.100"}
完整配置
yan.docCookie.has(key)
yan.docCookie.get(key)
yan.docCookie.set(key, val[,{ expires, path, domain, isSecure }])
yan.docCookie.remove(key[,{ path, domain }])
yan.http2({url[ ,method[ ,data[ ,responseType[ ,success[ ,error[ ,isAuth[ ,cancelToken[ ,timeout]]]]]]]]})
yan.getJSON(url,data,success,error);
yan.toFormatDate(fmt[ ,date])
Contributing
- Fork this Repo first
- Clone your Repo
- Install dependencies by
$ npm install
- Checkout a feature branch
- Feel free to add your features
- Make sure your features are fully tested
- Publish your local branch, Open a pull request
- Enjoy hacking <3
MIT license
Copyright (c) 2018 Yangfan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
built upon love by docor v0.3.0