Skip to content

Instantly share code, notes, and snippets.

@roshanca
roshanca / random.js
Created April 21, 2020 14:02
给定范围区间的随机 number 生成方法
/**
* Getting a random integer between two values, inclusive
* @param {number} min
* @param {number} max
* @return {number}
*/
export const getRandomIntInclusive = (min, max) => {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min + 1)) + min
@roshanca
roshanca / inherit.js
Created March 11, 2020 12:19
JS 继承 #js #prototype
function object(o) {
const F = function() {};
F.prototype = o;
return new F();
}
function extend(Child, Parent) {
var prototype = object(Parent.prototype);
prototype.constructor = Child;
Child.prototype = prototype;
@roshanca
roshanca / snippet.js
Created July 12, 2019 04:07
One loop two arrays #js
const exampleValues = [2, 15, 8, 23, 1, 32];
const [truthyValues, falseyValues] = exampleValues.reduce((arrays, exampleValue) => {
if (exampleValue > 10) {
arrays[0].push(exampleValue);
return arrays;
}
arrays[1].push(exampleValue);
return arrays;
}, [[], []]);
@roshanca
roshanca / cacheUtils.js
Created July 3, 2019 06:08
cache utils #js #cache
const Cache = {
setCache (key, val) {
if (!this.isIncognitoMode()) {
if (val === null) {
localStorage.removeItem(key)
} else {
localStorage.setItem(key, JSON.stringify(val))
}
} else {
// 兼容隐身模式
@roshanca
roshanca / translate.py
Created March 28, 2019 04:29
打点事件查询 Workflow
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from workflow import Workflow3, web
# Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入
reload(sys)
sys.setdefaultencoding('utf-8')
@roshanca
roshanca / mogujie.autoLogin.scpt
Last active March 28, 2019 04:22
mogu autologin
set MOGU_URL to "https://www.mogujie.com/"
set LOGIN_URL to "https://portal.mogujie.com/user/newlogin"
global TOGGLE_SELECTOR
global USERNAME_SELECTOR
global PASSWORD_SELECTOR
global LOGIN_BTN_SELECTOR
set WELCOME_SELECTOR to "#header > div.wrap.header-wrap.clearfix > div.search-nav-content.clearfix > div.site-top-nav.J_sitenav > div > div.header-user-info.fr"
set TOGGLE_SELECTOR to "#qrcode-wrapper > div.toggle-regular"
@roshanca
roshanca / flatten.js
Last active April 21, 2020 11:59
Array flatten #js #array
/**
* @param {Array<any>} arr
*/
function flatten(arr) {
return arr.reduce((flat, toFlatten) => {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten)
}, [])
}
@roshanca
roshanca / location.js
Created April 16, 2018 08:56
JavaScript 如何跳转页面
// 跳转
window.location.replace('https://www.awesomes.cn') // 不将被跳转页面加入浏览器记录
window.location.assign('https://www.awesomes.cn')
window.location.href = 'https://www.awesomes.cn'
window.location = 'https://www.awesomes.cn'
// 返回上一页
window.history.back()
window.history.go(-1)
@roshanca
roshanca / flexible.js
Created February 22, 2018 08:28
移动端根据屏幕尺寸计算 rem
;(function(win, lib) {
var doc = win.document;
var docEl = doc.documentElement;
var metaEl = doc.querySelector('meta[name="viewport"]');
var flexibleEl = doc.querySelector('meta[name="flexible"]');
var dpr = 0;
var scale = 0;
var tid;
var flexible = lib.flexible || (lib.flexible = {});
@roshanca
roshanca / utils.js
Last active March 26, 2019 08:18
实用工具
/**
* 获取表单数据对象
*
* @param {HTMLFormElement | string} form
* @return {object}
*/
export const getFormData = form => {
let data = {}
const kvObjArray = $(form).serializeArray()