Last active
November 29, 2018 09:38
-
-
Save thinkgarden/287fb3acd49aabc355931cf774dd6fcb to your computer and use it in GitHub Desktop.
[js functions] useful function for js #js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /userinfo/2144/id => ['/userinfo','/useinfo/2144,'/userindo/2144/id'] | |
export function urlToList(url) { | |
const urllist = url.split('/').filter(i => i); | |
return urllist.map((urlItem, index) => { | |
return `/${urllist.slice(0, index + 1).join('/')}`; | |
}); | |
} | |
// 极坐标转换成笛卡尔坐标 | |
function polarToCartesian(centerX, centerY, radius, angleInDegrees) { | |
let angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0 | |
return { | |
x: centerX + (radius * Math.cos(angleInRadians)), | |
y: centerY + (radius * Math.sin(angleInRadians)) | |
} | |
} | |
// Radom key | |
const key = Math.random().toString(36).substring(7); | |
// 获取 URL 参数 | |
const getURLParameters = url => (url.match(/([^?=&]+)(=([^&]*))/g) || []) | |
.reduce( | |
(a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),{} | |
); | |
const getURLParameter = name => decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [,''])[1].replace(/\+/g, '20%'))|| null; | |
// 获取 min-max 之间的随机整数 | |
const randomIntFromInterval = (min,max) => Math.floor(Math.random()*(max-min+1)+min); | |
// 正则表达式,只能是数字和字母 | |
const pattern =/^[A-Za-z0-9-_]+$/; | |
// 根据window.devicePixelRatio获取像素比 | |
function DPR() { | |
if (window.devicePixelRatio && window.devicePixelRatio > 1) { | |
return window.devicePixelRatio; | |
} | |
return 1; | |
} | |
// 将传入值转为整数 | |
function parseValue(value) { | |
return parseInt(value, 10); | |
}; | |
function generateSmall(){ | |
var str = []; | |
for(var i=97;i<123;i++){ | |
str.push(String.fromCharCode(i)); | |
} | |
return str; | |
} | |
function generateBig(){ | |
var str = []; | |
for(var i=65;i<91;i++){ | |
str.push(String.fromCharCode(i)); | |
} | |
return str; | |
} | |
function isPercentage(n) { | |
return typeof n === "string" && n.indexOf('%') != -1; | |
} | |
function convertToPercentage(n) { | |
if (n <= 1) { | |
n = (n * 100) + "%"; | |
} | |
return n; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment