Skip to content

Instantly share code, notes, and snippets.

View canwdev's full-sized avatar
🐟
Fishing

Canwdev canwdev

🐟
Fishing
View GitHub Profile
@canwdev
canwdev / get-tag-time.js
Last active July 8, 2021 10:08
获取日期和时间戳
function padNum(num, len = 2) {
return num.toString().padStart(len, '0')
}
/**
* 获取日期和时间戳(如:202009271518)
*/
function getDateTimeString(d = new Date()) {
return `${d.getFullYear()}${padNum(d.getMonth() + 1)}${padNum(d.getDate())}${padNum(d.getHours())}${padNum(d.getMinutes())}`
}
@canwdev
canwdev / safe-get-val.js
Created July 8, 2021 10:12
安全地获取嵌套对象的值
/**
* 安全地获取嵌套对象的值
* @param obj 对象
* @param path 键值数组
* @param fallback 获取失败的返回值
*/
function getVal(obj, path, fallback) {
try {
let i
const len = path.length
@canwdev
canwdev / parse-filename.js
Created July 8, 2021 10:14
获取文件名和后缀
// parseFileName('aaa_aa_xx.mp3')
const parseFileName = (n) => {
let dotIndex = n.lastIndexOf('.')
if (dotIndex < 0) {
dotIndex = n.length
}
const prefix = n.substr(0, dotIndex)
const suffix = n.substr(dotIndex, n.length)
return {
// dotIndex,
@canwdev
canwdev / guid.js
Last active November 17, 2021 10:01
生成随机 guid
const guid = () => {
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
}
return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
}
@canwdev
canwdev / lanhu-helper.js
Created July 10, 2021 03:46
获取蓝湖选中的文字并复制到控制台
function copyToClip(content) {
var aux = document.createElement("input");
aux.setAttribute("value", content);
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
}
function getLanhuSelectedText(splitter = ' ') {
@canwdev
canwdev / random-number.js
Last active November 20, 2021 10:50
获取随机数字
/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
/**
* Returns a random integer between min (inclusive) and max (inclusive).
* The value is no lower than min (or the next integer greater than min
@canwdev
canwdev / getincrementFilename.js
Last active November 17, 2021 10:00
获取自动递增文件名
const fs = require('fs-extra')
const Path = require('path')
// https://stackoverflow.com/a/65403443
const writeFileIncrement = async (filename, data, increment = 0) => {
const incrementText = increment ? `_${increment}` : ''
const name = `${Path.basename(filename, Path.extname(filename))}${incrementText}${Path.extname(filename)}`
return await fs.writeFileIncrement(name, data, {encoding: 'utf8', flag: 'wx'}).catch(async ex => {
if (ex.code === 'EEXIST') return await writeFileIncrement(filename, data, increment += 1)
throw ex
@canwdev
canwdev / leaflet-demo.js
Last active November 17, 2021 10:00
Leaflet插件 Vue Demo
export default {
methods: {
async initMap() {
import('leaflet/dist/leaflet.css')
import('leaflet').then((L) => {
const itemList = this.originalList || [{}]
const map = L.map('myMap').setView(itemList[0].latlng || [-25.363, 131.044], commonZoom)
const icon = L.icon({
iconUrl: require('~/assets/images/utils/map-icons/marker-icon.png'),
@canwdev
canwdev / genFolderTree.js
Created November 17, 2021 09:58
扁平文件夹数组生成文件夹树状结构
/**
* 扁平文件夹数组生成文件夹树状结构
* @param files
* @returns {*}
*/
function genFolderTree(files) {
const tree = []
const buildTree = (hierarchy, file) => {
let curTree = tree
const maxDepth = hierarchy.length - 1
@canwdev
canwdev / event-emitter.js
Created November 26, 2021 06:53
JS Class 事件触发器
export class EventEmitter {
constructor() {
this.events = []
}
on(name, fn) {
if (this.events[name]) {
this.events[name].push(fn)
} else {
this.events[name] = [fn]