Skip to content

Instantly share code, notes, and snippets.

@alanwei43
alanwei43 / tidy.hosts.js
Created February 13, 2019 06:11
tidy hosts content
#!/usr/bin/env node
const hostsFilePath = "/etc/hosts";
const fs = require("fs");
const path = require("path");
fs.readFile(hostsFilePath, (err, data) => {
let lines = data.toString().split("\n");
console.log("lines: " + lines.length);
let hosts = lines.map(line => line.replace(/\s+/g, " "))
@alanwei43
alanwei43 / getAllPropertyNames.md
Created May 8, 2019 14:31
获取对象的所有属性(包括原型链上的)

获取对象的所有属性(包括原型链上的)

function getAllPropertyNames(obj) {
    const props = [];
    do {
        if (obj === Object.prototype)
            break;
        Object.getOwnPropertyNames(obj).forEach(prop => {
 if (!props.includes(prop)) {
@alanwei43
alanwei43 / JavaScript Iterator.md
Last active May 13, 2019 05:38
JavaScript 迭代器
function* createGenerator() {
    let first = yield 1;
    let second = yield first + 2;
    yield second + 3;
}
let iterator = createGenerator();

iterator.next(); // { value: 1, done: false}
iterator.next(4);// { value: 6, done: false}
@alanwei43
alanwei43 / PrettyHosts.csx
Last active May 18, 2019 06:32
Pretty Hosts
#!/usr/bin/env dotnet-script
using System.Runtime.CompilerServices;
using System.IO;
using System.Text.RegularExpressions;
public static string GetScriptFolder([CallerFilePath] string path = null) => Path.GetDirectoryName(path);
static readonly var HOST_PATH = Environment.OSVersion.Platform == PlatformID.Unix ? "/etc/hosts" : @"C:\Windows\System32\drivers\etc\hosts";
public static async Task<IDictionary<String, List<String>>> GetPrettyHosts()
@alanwei43
alanwei43 / dwz.csx
Last active May 18, 2019 07:58
百度短网址生成脚本
#r "nuget: Newtonsoft.Json, 12.0.2"
using System.Net.Http;
using static System.Console;
using Newtonsoft.Json;
using static Newtonsoft.Json.JsonConvert;
if (Args.Count == 0)
{
Console.WriteLine($@"
@alanwei43
alanwei43 / push-to-gist.sh
Last active May 21, 2019 11:49
Push to gist 读取文件并发布到gist上
# 0. Your file name
FNAME="$2"
# 1. Somehow sanitize the file content
# Remove \r (from Windows end-of-lines),
# Replace tabs by \t
# Replace " by \"
# Replace EOL by \n
CONTENT=$(sed -e 's/\r//' -e's/\t/\\t/g' -e 's/"/\\"/g' "${FNAME}" | awk '{ printf($0 "\\n") }')
@alanwei43
alanwei43 / profiles.json
Created July 24, 2019 08:00
Microsoft Terminal Preview default configurations
{
"globals" :
{
"alwaysShowTabs" : true,
"defaultProfile" : "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"initialCols" : 120,
"initialRows" : 30,
"keybindings" :
[
{
@alanwei43
alanwei43 / repositories.md
Created August 4, 2019 08:03
Alpine Linux 镜像源

编辑 /etc/apk/repositories,然后在文件的最顶端添加(注意将 3.3 换成需要的版本)

http://mirrors.ustc.edu.cn/alpine/v3.3/main/

参考wiki

@alanwei43
alanwei43 / Promise.whenAllSettled.js
Created September 18, 2019 12:02
Promise.whenAllSettled
/**
* 所有Promise都被 settled
* @param {[{promise: function():Promise, data: {}}]} promises
* @retu {Promise<[{isResolve: boolean, isReject: boolean, error: {}, result: {}, data: {}}]>}
*/
Promise.whenAllSettled = function (promises) {
const isPromise = function (obj) {
return obj && typeof obj.then === "function" && typeof obj.catch === "function";
};
@alanwei43
alanwei43 / ChainResponse.js
Created September 21, 2019 04:41
ChainResponse.js
const fs = require("fs"), path = require("path");
let debugMode = false;
//#region 日志函数
function log(txt) {
if (debugMode) {
console.log(`[${new Date().toLocaleString()}] ${txt}`);
}
}
function warn(txt) {