npm.io
3.1.0 • Published 7 years ago

yanjs

Licence
MIT
Version
3.1.0
Deps
1
Size
291 kB
Vulns
22
Weekly
0
Stars
3

yanjs

NPM version Build Status Coverage Status

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 }])

prop value ps
key String cookie名
val String cookie值
expires Date|String(UTC) 过期时间
path String 默认为当前文档位置的路径,路径必须为绝对路径
domain String 默认为当前文档位置的路径的域名部分
isSecure Boolean cookie只会被https传输

yan.http2({url[ ,method[ ,data[ ,responseType[ ,success[ ,error[ ,isAuth[ ,cancelToken[ ,timeout]]]]]]]]}) yan.getJSON(url,data,success,error);

prop value ps
url String 请求地址
method String 请求方法,默认“GET”
headers Object 请求头信息
responseType String('arraybuffer','blob','document','json','text','stream') 请求返回数据的类型,默认“json”
data Object|String 请求数据
success Function 请求成功的回调,接受一个参数(data)
error Function 请求失败的回调,接受一个参数(err)
complete Function 无论成功失败,都会调用次函数,并且在success或error之前执行
isAuth Boolean 是否添加验证头信息,默认不添加
cancelAllRequest Boolean 是否取消其他进行中的请求
timeout Number(ms) 超时(单位毫秒)

yan.toFormatDate(fmt[ ,date])

prop value ps
fmt String 自定义格式(注意大小写) (“Y”:年,“M”:月,“D”:日,“h”:时,“i”:分,“s”:秒,“mss”:毫秒,“W”:星期,“q”:季度)
date 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