Skip to content

Instantly share code, notes, and snippets.

@CaptainLiao
CaptainLiao / urlencode.sh
Created December 29, 2018 04:30
在 bash 中实现 urlencode,转义中文字符
urlencode() {
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf "$c" | xxd -p -c1 | while read x;do printf "%%%s" "$x";done
esac
done
}
const UnimplementedError = createErrorType('Unimplemented');
const AuthError = createErrorType('AuthError');
const ResponseError = createErrorType('ResponseError', function(message, data) {
this.data = data;
});
module.exports = {
UnimplementedError,
var LOCALES = {
'zh_cn': {
weekday: function(i) { return '星期' + '日一二三四五六'[i] },
weekday_abbr: function(i) { return '周' + '日一二三四五六'[i] }
// 本地月日就免了,因为现在几乎看不到“一月二十三日”这种描述了
}
}
// XXX: ...默认中文
var LOCALE = LOCALES['zh_cn']
/*!
* decimal-js: Decimal Javascript Library v0.0.2
* https://github.com/shinuza/decimal-js/
*/
/*
Copyright (c) 2011 Samori Gorse, http://github.com/shinuza/decimal-js
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,
(function() {
window.onresize = function() {
setRootFontSize();
};
setRootFontSize();
function setRootFontSize() {
var el = document.documentElement;
var w = el.clientWidth;
// 最大按6Plus,最小按240,因为小于12px可能就有问题了
/**
* Returns a function, that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds. If `immediate` is passed, trigger the function on the
* leading edge, instead of the trailing. The function also has a property 'clear'
* that is a function which will clear the timer to prevent previously scheduled executions.
*
* @source underscore.js
* @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
* @param {Function} function to wrap
const type = require('./type.js')
const clone = require('./clone')
const SAME = '__DIFF_SAME__'
const REPLACE = '__DIFF_REPLACE__'
// 生成两个对象的比较结果
function diff(prev, next) {
if (prev === next) return SAME
function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
async function sleep(time) {
await timeout(time)
}
function throttle (method, context) {
clearTimeout(context.timerId);
/**
* EventBus 对象解决页面间的传值问题,在需要传值的页面引入这个文件
*
* 更新:9-29
* tigger 后自动注销事件,不需要手动管理
* 更新:10-20
* 支持发布-订阅、订阅-发布两种模式
* listen 第三个参数用于控制是否触发cache中的方法(为true开启订阅-发布模式)
*/
// 验证邮箱
function validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
// 验证中文字符
function validateCnText(str) {
var pattern = /^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/gi;
return pattern.test(str);