Skip to content

Instantly share code, notes, and snippets.

View shikelong's full-sized avatar
🏠
Working from home

shikelong shikelong

🏠
Working from home
View GitHub Profile
import * as React from 'react';
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { shallow, mount, render } from 'enzyme';
import {Home} from '../src/common/components/home/Home';
import {AxiosApi} from 'some-package'
import { Provider } from 'react-redux';
import MockAdapter from 'axios-mock-adapter';
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 -->
<html lang="zh-cmn-Hans"> <!-- 更加标准的 lang 属性写法 http://zhi.hu/XyIa -->
<head>
<!-- 声明文档使用的字符编码 -->
<meta charset='utf-8'>
<!-- 优先使用 IE 最新版本和 Chrome -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<!-- 页面描述 -->
<meta name="description" content="不超过150个字符"/>
<!-- 页面关键词 -->
@shikelong
shikelong / useControlledState
Created September 16, 2021 05:36
Hook help write Controlled Component Pattern
import * as React from 'react';
export default function useControlledState<T, R = T>(
defaultStateValue: T | (() => T),
option?: {
defaultValue?: T | (() => T);
value?: T;
onChange?: (value: T, prevValue: T) => void;
postState?: (value: T) => T;
}
const {PNG} = require('bundle.js');
const {Button, ImageView, ui} = require('tabris');
const base64 = require('base-64');
function _arrayBufferToBase64( buffer ) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
@shikelong
shikelong / checksession.html
Created November 12, 2021 04:11
check session example
<!DOCTYPE html>
<!--Copyright (c) Brock Allen & Dominick Baier. All rights reserved.-->
<!--Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.-->
<html>
<head>
<meta http-equiv='X-UA-Compatible' content='IE=edge' />
<title>Check Session IFrame</title>
</head>
<body>
@shikelong
shikelong / useInfiniteQuery.ts
Created November 15, 2021 08:13
[rtk query] useInfiniteQuery
import { QueryDefinition } from "@reduxjs/toolkit/dist/query";
import {
LazyQueryTrigger,
QueryHooks,
} from "@reduxjs/toolkit/dist/query/react/buildHooks";
import { useEffect, useState } from "react";
type GetResultTypeFromEndpoint<Endpoint> = Endpoint extends QueryHooks<
QueryDefinition<any, any, string, infer ResultType, string>
>
@shikelong
shikelong / convert-file-name.rb
Created November 17, 2021 01:25
【Ruby】 Convert file name (a b.svg ==> a-b.svg)
puts "start convert file names to hyphen style...."
def traverse_dir(fileType)
folder_path = File.absolute_path("./") + "/"
scheme = folder_path + "*.#{fileType}"
puts "find #{Dir.glob(scheme).length} #{fileType} files, start convert...."
puts '-' * 50
Dir.glob(scheme).each do |f|
filename = File.basename(f, File.extname(f))
File.rename(f, folder_path + filename.downcase.tr(' ', '-') + File.extname(f))
@shikelong
shikelong / async-await-forEach-alternatives.md
Created November 18, 2021 11:42 — forked from joeytwiddle/async-await-forEach-alternatives.md
Do not use forEach with async-await

Do not use forEach with async-await

TLDR: Use for...of instead of forEach in asynchronous code.

The problem

Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.)

For example, the following forEach loop might not do what it appears to do:

@shikelong
shikelong / useBlockRouteChange.ts
Created December 3, 2021 03:57
react-router Block Route Change
import { useEffect } from "react";
import { useHistory } from "react-router-dom";
export const defaultLevelMessage =
"未保存のデータがあります。ページを離れてもよろしいですか。";
function useBlockRouteChange(
shouldBlock: boolean,
message = defaultLevelMessage,
onLevel?: () => void
@shikelong
shikelong / attachDimensionStyleForImage.ts
Created December 7, 2021 03:05
Attach dimension style for img (get height/width value from height/width properties)
/**
* html-to-rtf-browser not recognize height/style property of img tag
* so, need add height/size to style property to avoid image lose rtf picture size.
*/
function attachDimensionStyleForImage(htmlString: string): string {
try {
const domParser = new DOMParser();
const doc = domParser.parseFromString(htmlString, "text/html");
const images = doc.getElementsByTagName("img");
for (let i = 0; i < images.length; i++) {