Skip to content

Instantly share code, notes, and snippets.

@roshanca
roshanca / randomStr.js
Created January 16, 2018 03:49
生成指定位数的随机数
/**
* 生成指定位数的随机数
* @param {number} x
*/
export const randomStr = (x) => {
let s = "";
while(s.length < x && x > 0){
let v = Math.random() < 0.5 ? 32 : 0;
s += String.fromCharCode(Math.round(Math.random() * ((122 - v) - (97 - v)) + (97 - v)));
}
@roshanca
roshanca / currency.js
Last active April 19, 2018 07:49
货币格式化展示
let num = 123456.1234
num.toLocaleString('zh-Hans-CN', { style: 'currency', currency: 'CNY' })
// ¥123,456.12
num.toLocaleString('en-US', { style: 'currency', currency: 'USD' })
// $123,456.12
num.toLocaleString('zh', { style: 'decimal', minimumFractionDigits: 2, maximumFractionDigits: 2 })
// 123,456.12
@roshanca
roshanca / .vimrc
Created July 18, 2017 13:01
My Vim Config
" 显示行号
set number
" 显示标尺
set ruler
" 历史纪录
set history=1000
" 输入的命令显示出来,看的清楚些
@roshanca
roshanca / users.js
Created June 20, 2017 06:34
Apollo’s asynchronous HoCs Example
const UniversalUser = universal(() => import('./User'), {
resolve: () => require.resolveWeak('./User'),
loading: <Loading />
})
const User = ({ loading, error, user }) =>
<div>
<UniversalUser isLoading={loading} error={error} user={user} />
</div>
@roshanca
roshanca / viewport-units.scss
Created May 20, 2017 01:47
Flexible layout with viewport unit
$vu_base: 375 // iphone6
@function vu($px) {
@return ($px / $vu_base) * 100vw
}
@roshanca
roshanca / security.js
Created May 2, 2017 01:47
Handle user's authentication with token
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken')
const config = require('../config/security')
function hashPassword(password) {
return new Promise((resolve, reject) => {
bcrypt.genSalt(config.saltRounds)
.then(salt => {
return bcrypt.hash(password, salt)
})
@roshanca
roshanca / batch.js
Created April 24, 2017 12:24
插入大量 DOM 的优化
const container = document.getElementById('js-list');
if (!container) {
return
}
const total = 10000
const batchSize = 4 // 每次插入多少个结点,越大越卡
const batchCount = total / batchSize // 插入次数
let batchDone = 0 // 已经完成的批处理个数
@roshanca
roshanca / unique.js
Last active June 22, 2018 08:30
unique array
const arr = ['aaa', 'bbb', 'aaa', '111'];
const uniqueArr = arr.filter((item, index) => {
return arr.indexOf(item) === index;
});
console.log(uniqueArr); // ['aaa', 'bbb', '111']
const simpleUnique = function(arr) {
return [...new Set(arr)]
}
@roshanca
roshanca / sort.js
Last active April 10, 2017 03:16
Array<Object> 根据 key 值进行排序 #tag: array, sort
const arr = [
{"name": "Kate", "age": 22},
{"name": "Candy", "age": 21},
{"name": "Petty", "age": 20}
];
arr.sort(compare('name')) // {"name": "Candy", "age": 21}, {"name": "Kate", "age": 22}, {"name": "Petty", "age": 20}
arr.sort(compare('age')); // {"name": "Petty", "age": 20}, {"name": "Candy", "age": 21}, {"name": "Kate", "age": 22}
function compare(key) {
@roshanca
roshanca / empty-array.md
Last active April 10, 2017 03:05
清空数组两种方式 #tag: array
var a = [1, 2, 3];
var b = a;
a = [];
console.log(a);    // []
console.log(b);    // [1,2,3]

a = [] 方式清除 a 变量数组引用其变量 b 的数组不变