Skip to content

Instantly share code, notes, and snippets.

@stiekel
stiekel / ksort.js
Created April 28, 2015 05:13
JavaScript ksort
function ksort(obj){
var keys = Object.keys(obj).sort()
, sortedObj = {};
for(var i in keys) {
sortedObj[keys[i]] = obj[keys[i]];
}
return sortedObj;
}
@stiekel
stiekel / gender_price.js
Created May 26, 2015 03:56
细细领会一下这是个什么程序的代码
var contact = [
{ nickname: 'coco', mobile: '13151185930', gender: '女' },
{ nickname: 'F**k', mobile: '18651941714', gender: '女' }
];
var price = [
{ mobile: '13151185930', price: 1024 },
{ mobile: '18651941714', price: 1722 }
];
contact.forEach(function(c){
@stiekel
stiekel / node_get_public_ip.js
Last active August 29, 2015 14:22
Get local server all public IPs
var os = require('os');
//get all public ips
var getMyPublicIp = function(){
var nics = os.networkInterfaces();
var ips = [];
for(var i in nics) {
nics[i].forEach(function(cfg){
if(!cfg.internal) {
ips.push(cfg.address);
}
@stiekel
stiekel / getPrevWorkDay.js
Created May 20, 2016 07:06
JavaScript get previous work day
function getPrevWorkDay (t) {
if(!t) t = new Date();
if( !(t instanceof Date) ) t = new Date(t);
var prevDay = new Date( t.getTime());
do {
prevDay = new Date( prevDay.getTime() - 86400000 );
} while( -1 !== [0, 6].indexOf(prevDay.getDay()) );
return prevDay;
}
@stiekel
stiekel / sleepSort.js
Last active March 3, 2017 03:14
JavaScript Sleep sort
var list = [-1, 3, 15, 133, -3, 2]
function sleepSort(arr) {
var ordered = []
var q = []
function afterWhile (x, resolve) {
ordered.push(x)
return resolve()
}
arr.forEach(n => {
@stiekel
stiekel / serialPromise.js
Created June 2, 2017 02:41
Run Multi Promise as Serial
var Promise = require('bluebird');
function getP (t) {
return new Promise((resolve, reject) => {
console.time(t);
setTimeout(() => {
console.log('\none Promise', t);
console.timeEnd(t);
return resolve();
}, t);
@stiekel
stiekel / jsonArrayToElasticsearch.js
Last active October 25, 2021 16:24
Import array in JSON file to Elasticsearch
// import json array file to elasticsearch
const fs = require('fs')
const shelljs = require('shelljs')
let endpoint = process.argv[2]
let jsonFile = process.argv[3]
if (!endpoint || !jsonFile || endpoint.search('_bulk') === -1) {
console.log('sample: node jsonArrayToES.js localhost:9200/bank/customer/_bulk customer.json')
process.exit()
}
@stiekel
stiekel / spinner.html
Created March 5, 2018 01:42
CSS Spinner
<!DOCTYPE html>
<html>
<head>
<title>Echarts</title>
<style type="text/css">
@keyframes loading-spinner {
0% {transform: rotate(0deg);}
20% {transform: rotate(120deg);}
40% {transform: rotate(240deg);}
50% {transform: rotate(270deg);}
@stiekel
stiekel / bus.js
Last active December 21, 2018 04:40
function Bus () {
this.events = {}
this.on = async (eventName, handler) => {
if (!this.events[eventName]) {
this.events[eventName] = []
}
let targetHandler = handler
let before = []
if (typeof handler === 'function') {
position = this.events[eventName].length
function parseAttrs (str) {
str = str.replace('(', '')
str = str.replace(')', '')
let parts = str.split(':')
let tmp = {}
if (parts[0] && parts[1]) {
let v = parts[1].trim()
v = v.replace(/\'/g, '')
if (!isNaN(v)) {
v = parseFloat(v)