Skip to content

Instantly share code, notes, and snippets.

@se7en00
se7en00 / main.ts
Created August 5, 2020 14:00 — forked from disintegrator/main.ts
RxJS + Axios
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { Observable } from 'rxjs';
const fromRequest = (request: AxiosRequestConfig) =>
new Observable<AxiosResponse>(
(o) => {
const source = axios.CancelToken.source();
o.add(() => source.cancel('Operation canceled by the user.'));
@se7en00
se7en00 / HttpStatusCode.ts
Created May 6, 2020 03:09 — forked from scokmen/HttpStatusCode.ts
Typescript Http Status Codes Enum
"use strict";
/**
* Hypertext Transfer Protocol (HTTP) response status codes.
* @see {@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes}
*/
enum HttpStatusCode {
/**
* The server has received the request headers and the client should proceed to send the request body
@se7en00
se7en00 / tsRecursive.md
Created September 9, 2019 02:52
TS 类型的递归

TS原生的Readonly只会限制一层写入操作,我们可以利用递归来实现深层次的Readonly。但要注意,TS对最大递归层数做了限制,最多递归5层。

type DeepReadony<T> = {
    readonly [P in keyof T]: DeepReadony<T[P]>
}

interface SomeObject {
  a: {
    b: {
 c: number;

我在项目中遇到这样一种场景,需要获取一个类型中所有value为指定类型的key。 例如,已知某个React组件的props类型

interface SomeProps {
    a: string
    b: number
    c: (e: MouseEvent) => void
    d: (e: TouchEvent) => void
}
@se7en00
se7en00 / .babelrc.js
Created July 21, 2019 13:50 — forked from nodkz/.babelrc.js
Babel 7.0 with .babelrc.js
/* eslint-disable prefer-template */
const path = require('path');
const aliases = require('./aliases');
// ///////////////////////////////////////////////////////////////
// ////////////////// PLUGINS ////////////////////////////////
// ///////////////////////////////////////////////////////////////
const commonPlugins = [
@se7en00
se7en00 / scroll
Last active June 10, 2019 07:26
页面垂直平滑滚动到指定滚动高度
/**
@description 页面垂直平滑滚动到指定滚动高度
@author zhangxinxu(.com)
*/
var scrollSmoothTo = function (position) {
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(callback, element) {
return setTimeout(callback, 17);
};
}
@se7en00
se7en00 / filterArraysRamda.md
Created February 28, 2019 06:24 — forked from cezarneaga/filterArraysRamda.md
Filter array of objects by nested values using ramda: Sometimes you dont have access to backend and you want to filter the response from an endpoint based on certain criteria. While trivial on flat arrays, this gets a bit tricky if the property you want to query is deeply nested. This is where Ramda shines.

Say we have a prop.users of the shape:

const users = [
    {username: 'bob', age: 30, tags: [{name: 'work', id: 1}, {name: 'boring', id: 2}]},
    {username: 'jim', age: 25, tags: [{name: 'home', id: 3}, {name: 'fun', id: 4}]},
    {username: 'jane', age: 30, tags: [{name: 'vacation', id: 5}, {name: 'fun', id: 4}]}
];

Either you use .babelrc to specify environment specific settings (plugins or transforms for example) using the env key:

{
  "presets": ["es2015", "stage-0", "react"],
  "env": {
    "development": {
      "plugins": [
        ["transform-object-rest-spread"],
 ["transform-react-display-name"],
@se7en00
se7en00 / gist:89b6e56fdacfb30fe5041ead48a1c0da
Created April 12, 2018 13:01
Array.filter(Boolean)简写
今天看到一个数组的声明是这样的:
var dotenvFiles = [
`${paths.dotenv}.${NODE_ENV}.local`,
`${paths.dotenv}.${NODE_ENV}`,
NODE_ENV !== 'test' && `${paths.dotenv}.local`,
paths.dotenv,
].filter(Boolean);
//result:
//['XX/XX.local', 'XX/XX']
@se7en00
se7en00 / capitalize .js
Last active February 9, 2018 03:15
capitalize first word
//大写单词的首字母
const capitalize = (str, lowerRest = false) => str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
// capitalize(‘myName’, true) -> ‘Myname’
//note: 使用slice(0,1)和toUpperCase()大写第一个字母,slice(1)获取字符串的其余部分.
//大写每个单词的首字母
const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());