Skip to content

Instantly share code, notes, and snippets.

View guxuerui's full-sized avatar

Gu Xuerui guxuerui

View GitHub Profile
@guxuerui
guxuerui / fib.js
Last active June 22, 2020 09:16
菲波那切数列
// const fib = (num) => {
// if (num <= 0) return 0;
// if (num === 1 || num === 2) return 1;
// if (num > 2) {
// let prev = 1, next = 1;
// for (let i = 3; i <= num; i++) {
// let sum = prev + next;
// prev = next;
// next = sum;
// }
@guxuerui
guxuerui / getSum.js
Created July 2, 2020 06:38
js两数求和
const sum = (a, b) => {
if (a == 0) return b;
if (b == 0) return a;
let newA = a ^ b;
let newB = (a & b) << 1;
return sum(newA, newB);
}
const a = 10, b = 5;
console.log(sum(a, b)); // 15
@guxuerui
guxuerui / getRes.js
Last active July 9, 2020 07:39
笔试题--JS中的this
var length = 10;
function fn() {
console.log(this.length);
}
var obj = {
length: 5,
method: function(fn) {
fn();
arguments[0]();
@guxuerui
guxuerui / getThisRes.js
Created July 9, 2020 07:52
笔试题--JS中的this
var name = 0;
var a = {
name: 1,
f1: function(){
this.name = 2;
},
f2: function(){
this.name = 3;
return null
},
@guxuerui
guxuerui / judgeDataType.js
Created August 11, 2020 08:01
JS正确判断数据类型
function isType(target, type) {
let targetType = Object.prototype.toString.call(target).slice(8, -1).toLowerCase()
return targetType === type.toLowerCase()
}
isType([], 'Array') // true
isType(/\d/, 'RegExp') // true
isType(new Date(), 'Date') // true
isType(function(){}, 'Function') // true
isType(Symbol(1), 'Symbol') // true
@guxuerui
guxuerui / trim.js
Created August 17, 2020 09:41
正则实现trim()功能去除字符串前后空格
function myTrim(str){
let reg = /^\s+|\s+$/g;
return str.replace(reg,"");
}
console.log(myTrim(" abcd ")); // abcd
@guxuerui
guxuerui / arrToTree.js
Created July 15, 2021 10:55
将扁平数组转化为Tree
const arrayToTree = (items) => {
let results = [];
let map = {};
items.forEach(el => {
let item = {...el, children: []};
if (map[item.id]) {
item.children = map[item.id];
} else {
map[item.id] = item.children;
@guxuerui
guxuerui / config.js
Last active August 15, 2022 03:02
vite和vue3项目中配置打包后服务请求地址
// in public/config.js
window.g = {
protocol: 'http',
address: '192.168.20.229',
port: 18000,
timeout: 12 * 1000
}
// in http/index.ts
@guxuerui
guxuerui / getMdFileMeta.js
Created March 28, 2023 04:50
使用vite,导入markdown文件后读取定义的meta信息
const getMetaData = async () => {
// example file path: ./pages/js/*.md
const allPostFiles = import.meta.glob('./pages/js/*.md')
const iterablePostFiles = Object.entries(allPostFiles)
const allPosts = await Promise.all(
iterablePostFiles.map(async ([path, resolver]) => {
const resolvedPost = await resolver()
return {
@guxuerui
guxuerui / CopyCodeBlock.ts
Created May 25, 2023 01:51
Copy code block content to clipboard
import { copyToClipboard } from 'utils-snap-fn'
const copyIcon = `
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 32 32"><path fill="currentColor" d="M28 10v18H10V10h18m0-2H10a2 2 0 0 0-2 2v18a2 2 0 0 0 2 2h18a2 2 0 0 0 2-2V10a2 2 0 0 0-2-2Z"/><path fill="currentColor" d="M4 18H2V4a2 2 0 0 1 2-2h14v2H4Z"/></svg>
`
const copiedIcon = `
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 32 32"><path fill="#00a98e" d="m14 21.414l-5-5.001L10.413 15L14 18.586L21.585 11L23 12.415l-9 8.999z"/><path fill="#00a98e" d="M16 2a14 14 0 1 0 14 14A14 14 0 0 0 16 2Zm0 26a12 12 0 1 1 12-12a12 12 0 0 1-12 12Z"/></svg>
`