Skip to content

Instantly share code, notes, and snippets.

View nuintun's full-sized avatar
😊
I may be slow to respond.

nuintun nuintun

😊
I may be slow to respond.
View GitHub Profile
@nuintun
nuintun / GUID.md
Last active June 6, 2016 06:10
GUID生成
// GUID
Math.trueRandom = (function (){
  if (crypto.getRandomValues) {
    // if we have a crypto library, use it
    return function (){
      var array = new Uint32Array(1);
      
      crypto.getRandomValues(array);
      
@nuintun
nuintun / uuid.js
Created December 6, 2014 12:44
UUID
/*!
Math.uuid.js (v1.4)
http://www.broofa.com
mailto:[email protected]
Copyright (c) 2010 Robert Kieffer
Dual licensed under the MIT and GPL licenses.
*/
/**
@nuintun
nuintun / base64.js
Last active February 11, 2017 13:25
用 XMLDOM 和 ADODB.Stream 实现base64编码解码
/**
* Base64
* http://blog.csdn.net/cuixiping/article/details/409468
* ADODB.Stream 实例有 LoadFromFile 方法可以读取文件内容
* ADODB.Stream 实例有 state 属性 0 和 1,分别对应 Open 状态: adStateClosed 和 adStateOpen
* ADODB.Stream 实例有 LineSeparator 属性 13、-1 和 10,分别对应换行符: adCR、adCRLF 和 adLF
* ADODB.Stream 实例的其他属性和方法可参考相应文档
*/
var Base64 = {
/**
@nuintun
nuintun / strip-bom.md
Created February 11, 2017 13:23
过滤utf8 BOM
/**
 * strip utf-8 BOM
 *
 * @param {any} buffer
 * @returns {Buffer}
 */
function stripBOM(buffer) {
  //EF BB BF 239 187 191
 if (buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) {
@nuintun
nuintun / pad.md
Created March 23, 2017 09:35
字符右补白
'use strict'

// 兼容模式,兼容老版本浏览器
function pad(value, length, placeholder) {
  length = length || 15;
  placeholder = placeholder || ' ';

  var diff = Math.max(0, length - value.length) + 1;
@nuintun
nuintun / interceptor.js
Created October 11, 2018 09:15
IISNode Interceptor
(function() {
// refactor process.argv to determine the app entry point and remove the interceptor parameter
var appFile;
var newArgs = [];
process.argv.forEach(function(item, index) {
if (index === 2) appFile = item;
if (index !== 1) newArgs.push(item);
});
@nuintun
nuintun / UTF8.js
Last active April 22, 2020 15:11
UTF8 编码速度测试
// https://jsperf.com/utf8-encoder-decoder
var encoder = new TextEncoder();
var decoder = new TextDecoder();
// https://developer.mozilla.org/zh-CN/docs/Web/API/TextEncoder
var native = {
encode: encoder.encode.bind(encoder),
decode: decoder.decode.bind(decoder)
};
@nuintun
nuintun / LCGRandom.js
Created May 25, 2020 02:30
线性同余随机数
/**
* @function LCGRandom
* @description 线性同余随机数
* @param {number} seed
* @returns {number}
* @see https://gist.github.com/Protonk/5389384
*/
function LCGRandom1(seed = 0) {
return ((seed * 11 + 17) % 25) / 25;
}
@nuintun
nuintun / hex-rgb.js
Created July 2, 2020 08:05
hex 和 rgb 颜色转换
function rgb2hex(red, green, blue) {
const rgb = (red << 16) | (green << 8) | blue;
return `#${rgb.toString(16).padStart(6, '0')}`;
}
function hex2rgb(hex) {
return [(hex >> 16) & 0xff, (hex >> 8) & 0xff, hex & 0xff];
}
@nuintun
nuintun / hashCode.ts
Last active March 11, 2021 02:21
Java 中字符串 hashCode 方法的 JavaScript 实现
/**
* @function hashCode
* @param {string} string
* @returns {number}
*/
function hashCode(string: string): number {
let hash: number = 0;
const { length }: string = string;