Skip to content

Instantly share code, notes, and snippets.

View SuperAL's full-sized avatar

Alessia Li SuperAL

View GitHub Profile
@SuperAL
SuperAL / jsonFormatter.js
Last active April 2, 2018 09:27
格式化 json 对象
// 参考:https://stackoverflow.com/questions/6937863/json-stringify-so-that-arrays-are-on-one-line
// 元素皆为原始类型的数组不换行
// 举例说明:元素皆为原始类型的数组格式化为
// a: [1, 2, 3]
// 而不是
// a: [
// 1,
// 2,
// 3
// ]
@SuperAL
SuperAL / sleep.js
Created March 26, 2018 07:21
javascript 实现 sleep 函数
// 链接:https://www.zhihu.com/question/31636244/answer/52835780
// 来源:知乎
async function test() {
console.log('Hello')
await sleep(1000)
console.log('world!')
}
function sleep(ms) {
@SuperAL
SuperAL / getType.js
Created February 27, 2018 10:04
获取值类型
// Reference:https://github.com/jkchao/blog/issues/8
function getType (val) {
const str = Object.prototype.toString.call(val)
return /^\[object (.*)\]$/.exec(str)[1]
}
@SuperAL
SuperAL / react-component.jsx
Last active February 27, 2018 09:50
Simple Notes
class MyComp extends Component {
// default state
state = { fetching: 0 }
componentWillMount() {
// Setting state here won't trigger re-render
this.setState({ fetching: 1 });
}
@SuperAL
SuperAL / setParam.js
Created February 1, 2018 07:27
替换 url 参数的值
// use: setParam('...?a=2&b=4', 'a', 3);
function setParam(uri, key, val) {
return uri
.replace(new RegExp("([?&]"+key+"(?=[=&#]|$)[^#&]*|(?=#|$))"), "&"+key+"="+encodeURIComponent(val))
.replace(/^([^?&]+)&/, "$1?");
}
@SuperAL
SuperAL / numberWithCommas.js
Created December 7, 2017 09:42
数字千分位逗号处理(ES6)
/**
* numberWithCommas 数字千分位逗号处理
* @param {Number} x - 需要处理的数字
* @return {String} - 处理后的数字
*/
export default function numberWithCommas(x) {
if (!x) {
return '--';
}
let parts = x.toString().split(".");
@SuperAL
SuperAL / lazyloadImage.js
Created December 5, 2017 01:53
单个图片懒加载
/*
Lazy Loading Images
https://css-tricks.com/snippets/javascript/lazy-loading-images/
*/
function loadImage (el, fn) {
var img = new Image()
, src = el.getAttribute('data-src');
img.onload = function() {
if (!! el.parent)
@SuperAL
SuperAL / dateDuration.js
Created September 18, 2017 08:53
获取时间区间的跨度天数
// stackoverflow: https://stackoverflow.com/a/2627493/6339408
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,22);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay))); // -> 10
@SuperAL
SuperAL / secondsFormatter.js
Created August 16, 2017 06:55
format seconds and in reverse
// seconds to hh:mm:ss
// use: alert("5678".toHHMMSS());
String.prototype.toHHMMSS = function () {
var sec_num = parseInt(this, 10); // don't forget the second param
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
@SuperAL
SuperAL / setElementMinHeight.js
Created August 15, 2017 09:41
set element min-height based on body height
$(function(){
var minHeight = document.body.offsetHeight;
document.getElementById("main").style.minHeight = minHeight+'px';
document.getElementById("sidebar").style.minHeight = minHeight+'px';
})