Skip to content

Instantly share code, notes, and snippets.

@yy-dev7
yy-dev7 / machine.js
Last active January 27, 2021 02:39
Generated by XState Viz: https://xstate.js.org/viz
const surveyMachine = Machine({
id: 'survey',
initial: 'index',
states: {
index: {
on: {
VERIFICATION_ACTIVE: 'verification',
VERIFICATION_INACTIVE: 'start',
@yy-dev7
yy-dev7 / regxp.js
Created January 9, 2018 06:54
常用js正则
//正整数
/^[0-9]*[1-9][0-9]*$/;
//负整数
/^-[0-9]*[1-9][0-9]*$/;
//正浮点数
/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/;
//负浮点数
@yy-dev7
yy-dev7 / transformRMB.js
Last active January 9, 2018 06:52
金额大写转换函数
function transformRMB(tranvalue) {
//拆分整数与小数
var splits = function(tranvalue) {
var value = new Array('', '');
temp = tranvalue.toString().split(".");
for (var i = 0; i < temp.length; i++) {
value = temp;
}
return value;
};
@yy-dev7
yy-dev7 / timeFormat.js
Last active January 9, 2018 06:52
时间个性化输出
/*
1、< 60s, 显示为“刚刚”
2、>= 1min && < 60 min, 显示与当前时间差“XX分钟前”
3、>= 60min && < 1day, 显示与当前时间差“今天 XX:XX”
4、>= 1day && < 1year, 显示日期“XX月XX日 XX:XX”
5、>= 1year, 显示具体日期“XXXX年XX月XX日 XX:XX”
*/
function timeFormat(time) {
var date = new Date(time),
curDate = new Date(),
@yy-dev7
yy-dev7 / clone.js
Last active March 9, 2018 02:06
deep clone
function deepClone() {
let copy
// 处理3个简单的类型, null 或者 undefined
if (obj === null || typeof obj !== 'object') {
return obj
}
if (obj instanceof Date) {
copy = new Date()
@yy-dev7
yy-dev7 / throttle.js
Created January 9, 2018 06:21
throttle
function throttle(fn, delay = 200) {
let now
let lastExec
let timer
let context
let args
const execute = () => {
fn.apply(context, args)
lastExec = now
@yy-dev7
yy-dev7 / storage.js
Created January 9, 2018 06:20
可以为sessionStorage,localStorage设置过期时间
class Storage {
constructor(strategy = 'internal') {
this.strategy = strategy
}
/**
* @param {string} key
* @param {any} val
* @param {number} maxAge 存储时间:ms
*/
@yy-dev7
yy-dev7 / getUrlParams.js
Created January 9, 2018 06:18
获取URL参数
/**
* 获取URL参数
* // http://jsfiddle.net/draft/?foo=foo&bar=bar
console.log(getUrlParams('foo')); // "foo"
console.log(getUrlParams()); // {foo: "foo", bar: "bar"}
*/
function getUrlParams(name, uri) {
const params = {}
const search = uri || window.location.search
@yy-dev7
yy-dev7 / scrollToY.js
Last active June 19, 2017 06:57
Y轴滑动
window.requestAnimFrame = (function requestAnimFrame() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function simulation(callback) {
window.setTimeout(callback, 1000 / 60)
}
}())
function scrollToY(scrollTargetY = 0, speed = 2000, easing = 'easeOutSine') {
function optionalChaining(obj, chain) {
return chain
.split('.')
.reduce(function(acc, val) {
return acc ? acc[val] : undefined;
}, obj);
}
var user = { address: { street: 'No.969 West WenYi Road', }, a: { b: { c: 2 } }, }
var ret = optionalChaining(user, 'address');
console.log(ret)