Skip to content

Instantly share code, notes, and snippets.

@liuwenzhuang
liuwenzhuang / objectEntries.js
Created July 19, 2018 05:54
使原生Object能够被遍历
/**
* 使object可遍历
* @param {Object} obj 需要处理的object
*/
function* objectEntries(obj) {
const propKeys = Reflect.ownKeys(obj);
for(const propKey of propKeys) {
yield [propKey, obj[propKey]];
}
}
@liuwenzhuang
liuwenzhuang / beforeunload.js
Created October 19, 2018 06:08
离开页面前的监听事件
window.addEventListener("beforeunload", function (event) {
if (!checkSomething()) { // 假设已存在此方法
// 标准
event.preventDefault();
// Chrome需要
event.returnValue = '';
}
});
@liuwenzhuang
liuwenzhuang / addSetGet2localStorage.js
Created March 6, 2019 09:08
Add set/get object capacity to localStorage 为localStorage增加读、写Object的能力
if (typeof Storage === 'undefined') {
window.localStorage = {};
localStorage.setItem = function (key, val) {
this[key] = val;
};
localStorage.getItem = function (key) {
return this[key];
};
window.localStorage.setObject = function (key, value) {
this[key] = value;
@liuwenzhuang
liuwenzhuang / add_schemes_windows_terminal.ps1
Last active January 11, 2020 12:19
为wsl添加主题.(add schemes from iTerm2-Color-Schemes to wsl profiles). Code from https://andrewpla.dev/Adding-New-Color-Schemes-To-Windows-Terminal/
# Path to the profile when installed from the Windows Store.
$profilePath = "C:\Users\$Env:Username\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\profiles.json"
# remove comments
$profile = (Get-Content $ProfilePath) -replace '(?m)(?<=^([^"]|"[^"]*")*)//.*' -replace '(?ms)/\*.*?\*/' | Out-String | ConvertFrom-Json
$backupProfilePath = "$home\Documents\WindowsTerminalprofiles.json"
Write-Verbose "Backing up profile to $backupProfilePath"
$profile | ConvertTo-Json | Set-Content $backupProfilePath
@liuwenzhuang
liuwenzhuang / .vimrc
Last active January 19, 2020 01:52
vim config file, place it to user directory
syntax on
set autoindent
set expandtab
set tabstop=2
set sw=2
set showcmd
set shortmess=at
set nowritebackup
@liuwenzhuang
liuwenzhuang / profiles.json
Created January 19, 2020 02:15
Add Git Bash to windows terminal. Windows Terminal添加Git Bash
{
"profiles": [
{
"guid": "{61c54bbd-c2c6-5271-96e7-109a87ff44be}",
"name": "Git Bash",
"commandline": "%ProgramFiles%\\Git\\bin\\bash.exe --login -i",
"icon": "%ProgramFiles%\\Git\\mingw64\\share\\git\\git-for-windows.ico",
"startingDirectory": "%USERPROFILE%",
"hidden": false
},
@liuwenzhuang
liuwenzhuang / launch.json
Created June 5, 2020 07:11
launch and debug configuration for node, .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"protocol": "inspector",
"name": "Attach",
"port": 9229
}
@liuwenzhuang
liuwenzhuang / binary-search-tree.ts
Created July 31, 2020 09:04
binary search tree in typescript
// tslint:disable: max-classes-per-file
class TreeNode {
public left: TreeNode = null;
public right: TreeNode = null;
constructor(public data) {
this.data = data;
this.left = null;
this.right = null;
}
}
@liuwenzhuang
liuwenzhuang / graph.ts
Created August 3, 2020 03:44
typescript realize graph by adjacency list
class Graph {
public adjList = new Map<any, any[]>();
public numberOfVertexs: number = 0;
public addVertex(vertex) {
this.adjList.set(vertex, []);
this.numberOfVertexs ++;
}
public addEdge(vertex1, vertex2) {
@liuwenzhuang
liuwenzhuang / main.js
Last active October 23, 2020 08:54
worker and main thread communicate by postMessage and addEventListener
const worker = new Worker('worker.js')
// handle data from worker
worker.addEventListener('message', (e) => {
console.log('worker send data back:', e.data)
}, false);
// handle error
worker.addEventListener('error', (err) => {
console.log(`Error: Line ${err.lineno} in ${err.filename} : ${e.message}`);