Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀
努力工作中

张树源 mrxf

🚀
努力工作中
View GitHub Profile
@mrxf
mrxf / download.tsx
Last active Nov 9, 2020
xhr download file
View download.tsx
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 Mar 26, 2020
typescript函数重载
View ts-overload.ts
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 Jan 30, 2018
compare two differ between two Array
View compare.js
const lastMonthData = [
{
id: 1,
name: "Alex",
salary: 15662
},
{
id: 2,
name: "Tom",
salary: 18672
@mrxf
mrxf / global-storage.js
Created Jan 29, 2018
store your date in global
View global-storage.js
/**
* tool for store data in global
*/
class GlobalStorage{
/**
* init Global Storage
* @param {string} globalKey key for store
*/
constructor(globalKey = "app") {
this.globalKey = globalKey;
View get-param-by-name.js
/**
* 根据参数名获取参数值
* @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 Jul 28, 2017
使用article parser提取网页内容
View parser-article.js
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 Jul 28, 2017
获取medium中的音频media
View get-medium-media.js
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) => {
View symbol-defineProperty-excise.js
/**
* 定义一个函数,里面带有一个方法
* 在不修改该方法的条件下,获取到o中所有的属性名
*/
var foo = (function(){
var o = {
a: 1,
b: 2,
/**更多属性**/
};
View @sleep.ts
/**
* 控制一个函数延后执行
* @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)
@mrxf
mrxf / ripple.js
Created Apr 25, 2017
material ripple effect
View ripple.js
function RippleEffect(element){
this.element = element;
this.element.addEventListener('mousedown', this.run.bind(this), false);
}
RippleEffect.prototype = {
run: function(event){
var ripplerContainer = this.element.querySelector('.ripple-container');
var offsetInfo = this.element.getBoundingClientRect();
var rippleContainer = document.createElement('div');