Skip to content

Instantly share code, notes, and snippets.

View mrxf's full-sized avatar
🚀
努力工作中

张树源 mrxf

🚀
努力工作中
View GitHub Profile
@mrxf
mrxf / download.tsx
Last active November 9, 2020 07:42
xhr download file
import { Button, Card, message, Progress, Space } from "antd";
import Axios, { CancelTokenSource } from "axios";
import contentDisposition from "content-disposition";
import React, { useCallback, useRef, useState } from "react";
interface DownloadProps {}
const Download: React.FC<DownloadProps> = () => {
const cancelSource = useRef<CancelTokenSource | null>(null);
const [percentage, setPercentage] = useState<number>(0);
@mrxf
mrxf / ts-overload.ts
Created March 26, 2020 08:13
typescript函数重载
export class TestClass {
someMethod(): number;
someMethod(str: string): string;
someMethod(str?: string) {
if ( typeof str === 'string' ) {
return str + '123';
} else {
return Math.random();
}
@mrxf
mrxf / compare.js
Created January 30, 2018 03:14
compare two differ between two Array
const lastMonthData = [
{
id: 1,
name: "Alex",
salary: 15662
},
{
id: 2,
name: "Tom",
salary: 18672
@mrxf
mrxf / global-storage.js
Created January 29, 2018 08:36
store your date in global
/**
* tool for store data in global
*/
class GlobalStorage{
/**
* init Global Storage
* @param {string} globalKey key for store
*/
constructor(globalKey = "app") {
this.globalKey = globalKey;
@mrxf
mrxf / get-param-by-name.js
Created January 29, 2018 06:37
url操作
/**
* 根据参数名获取参数值
* @param {string} name 参数名
* @param {string} [url] 地址
*/
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
@mrxf
mrxf / parser-article.js
Created July 28, 2017 02:32
使用article parser提取网页内容
var ArticleParser = require('article-parser');
var he = require('he');
var url = 'http://mp.weixin.qq.com/s/JJbZkcBoSMW0zPzmepUr1A';
ArticleParser.configure({
htmlRules:{
allowedTags: [ 'pre', 'p', 'img' ],
allowedAttributes: {
pre: ['style'],
@mrxf
mrxf / get-medium-media.js
Created July 28, 2017 02:24
获取medium中的音频media
const request = require('request')
const cheerio = require('cheerio')
const url = 'https://medium.com/@leighalexander/the-underground-world-of-magical-resistance-on-the-internet-6c854e567347';
const audioUrl = /https:\/\/cdn-audio.*m4a/g;
const reqOpt = {
url: url
}
request(reqOpt, (error, res, body) => {
/**
* 定义一个函数,里面带有一个方法
* 在不修改该方法的条件下,获取到o中所有的属性名
*/
var foo = (function(){
var o = {
a: 1,
b: 2,
/**更多属性**/
};
@mrxf
mrxf / @sleep.ts
Last active June 12, 2017 09:33
@decorator 装饰器
/**
* 控制一个函数延后执行
* @param tick 休眠时间
*/
function sleep(tick: number){
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
let method = descriptor.value;
descriptor.value = (...args: any[]) =>{
setTimeout(()=>{
method.apply(target, args)