diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-06-05 23:56:12 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-06-05 23:56:12 +0200 |
| commit | 8c8e2e08d2ae89ba18ca05bab446e4642798dce2 (patch) | |
| tree | d3153ba1fc32faa19d4094b2b5a18943f1e45dfa /app/client/util | |
| parent | c4dd63e487588cfa8e104a840af75854972b5ddb (diff) | |
pulling in wav2pix code
Diffstat (limited to 'app/client/util')
| -rw-r--r-- | app/client/util/format.js | 132 | ||||
| -rw-r--r-- | app/client/util/index.js | 146 | ||||
| -rw-r--r-- | app/client/util/math.js | 52 |
3 files changed, 189 insertions, 141 deletions
diff --git a/app/client/util/format.js b/app/client/util/format.js new file mode 100644 index 0000000..e436a3e --- /dev/null +++ b/app/client/util/format.js @@ -0,0 +1,132 @@ +export function timeInSeconds(n){ + return (n / 10).toFixed(1) + ' s.' +} +export function gerund(s){ + return s.replace(/e?$/, 'ing') +} +export function commatize (n, radix) { + radix = radix || 1024 + var nums = [], i, counter = 0, r = Math.floor + if (n > radix) { + n /= radix + nums.unshift(r((n * 10) % 10)) + nums.unshift(".") + } + do { + i = n % 10 + n = r(n / 10) + if (n && ! (++counter % 3)) + { i = ' ' + r(i) } + nums.unshift(r(i)) + } + while (n) + return nums.join("") +} +export function carbon_date (date, no_bold) { + var span = (+new Date() - new Date(date)) / 1000, color + if (! no_bold && span < 86400) // modified today + { color = "new" } + else if (span < 604800) // modifed this week + { color = "recent" } + else if (span < 1209600) // modifed 2 weeks ago + { color = "med" } + else if (span < 3024000) // modifed 5 weeks ago + { color = "old" } + else if (span < 12315200) // modifed 6 months ago + { color = "older" } + else + { color = "quiet" } + return color +} +export function hush_views (n, bias, no_bold) { + var txt = commatize(n, 1000) + bias = bias || 1 + n = n || 0 + if (n < 30) { return["quiet", n + " v."] } + if (n < 200) { return ["quiet", txt + " v."] } + else if (n < 500) { return ["quiet", txt + " v."] } + else if (n < 1000) { return ["old", txt + " v."] } + else if (n < 5000) { return ["med", txt + " kv."] } + else if (no_bold || n < 10000) { return ["recent", txt + " kv."] } + else { return ["new", txt + " kv."] } +} +export function hush_threads (n, bias, no_bold) { + var txt = commatize(n, 1000) + bias = bias || 1 + n = n || 0 + if (n < 10) { return["quiet", n + " t."] } + else if (n < 25) { return ["old", txt + " t."] } + else if (n < 50) { return ["med", txt + " t."] } + else if (no_bold || n < 100) { return ["recent", txt + " t."] } + else { return ["new", txt + " t."] } +} +export function hush_size (n, bias, no_bold) { + var txt = commatize(Math.round(n / 1024)) + bias = 1 || bias + n = n || 0 + if (! n) { return ['', ''] } + if (n < 1000) { + return ["quiet", n + " b."] + } + if (n < 1000000) { + return ["quiet", txt + " kb."] + } + else if (n < (20000000/bias)) { + return ["quiet", txt + " mb."] + } + else if (n < (50000000/bias)) { + return ["old", txt + " mb."] + } + else if (n < (80000000/bias)) { + return ["med", txt + " mb."] + } + else if (no_bold || n < (170000000/bias)) { + return ["recent", txt + " mb."] + } + else { + return ["new", txt + " mb."] + } +} +export function hush_null (n, unit, no_bold) { + var s = unit ? n + " " + unit + "." : n + if (n < 3) { + return ["quiet", s] + } + else if (n < 6) { + return ["older", s] + } + else if (n < 10) { + return ["old", s] + } + else if (n < 16) { + return ["med", s] + } + else if (no_bold || n < 21) { + return ["recent", s] + } + else { + return ["new", s] + } +} +export function get_age (t) { + var age = Math.abs(+Date.now() - new Date(t))/1000 + var r = Math.floor + var m + if (age < 5) { return "now" } + if (age < 60) { return r(age) + "s" } + age /= 60 + if (age < 60) { return r(age) + "m" } + m = r(age % 60) + age /= 60 + if (m > 0 && age < 2) { return r(age) + "h" + m + "m" } + if (age < 24) { return r(age) + "h" } + age /= 24 + if (age < 7) { return r(age) + "d" } + age /= 7 + if (age < 12) { return r(age) + "w" } + age /= 4 + if (age < 12) { return r(age) + "m" } + age /= 12 + return r(age) + "y" +} +export function courtesy_s (n, s) { return n == 1 ? "" : (s || "s") } diff --git a/app/client/util/index.js b/app/client/util/index.js index c5eb8dd..a811dcf 100644 --- a/app/client/util/index.js +++ b/app/client/util/index.js @@ -1,7 +1,11 @@ import * as sort from './sort' +import * as format from './format' +import * as maths from './math' export { - sort + sort, + ...maths, + ...format, } export const is_iphone = !!((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) @@ -16,13 +20,6 @@ htmlClassList.remove('loading') // window.debug = false -export function clamp(n,a,b) { return n<a?a:n<b?n:b } -export function norm(n,a,b) { return (n-a) / (b-a) } -export function lerp(n,a,b) { return (b-a)*n+a } -export function mix(n,a,b) { return a*(1-n)+b*n } -export function randint(n) { return Math.floor(Math.random()*n) } -export function randrange(a,b){ return Math.random() * (b-a) + a } - document.body.style.backgroundImage = 'linear-gradient(' + (randint(40)+40) + 'deg, #fde, #ffe)' export const allProgress = (promises, progress_cb) => { @@ -37,136 +34,3 @@ export const allProgress = (promises, progress_cb) => { }) return Promise.all(promises) } - -export function timeInSeconds(n){ - return (n / 10).toFixed(1) + ' s.' -} -export function gerund(s){ - return s.replace(/e?$/, 'ing') -} -export function commatize (n, radix) { - radix = radix || 1024 - var nums = [], i, counter = 0, r = Math.floor - if (n > radix) { - n /= radix - nums.unshift(r((n * 10) % 10)) - nums.unshift(".") - } - do { - i = n % 10 - n = r(n / 10) - if (n && ! (++counter % 3)) - { i = ' ' + r(i) } - nums.unshift(r(i)) - } - while (n) - return nums.join("") -} -export function carbon_date (date, no_bold) { - var span = (+new Date() - new Date(date)) / 1000, color - if (! no_bold && span < 86400) // modified today - { color = "new" } - else if (span < 604800) // modifed this week - { color = "recent" } - else if (span < 1209600) // modifed 2 weeks ago - { color = "med" } - else if (span < 3024000) // modifed 5 weeks ago - { color = "old" } - else if (span < 12315200) // modifed 6 months ago - { color = "older" } - else - { color = "quiet" } - return color -} -export function hush_views (n, bias, no_bold) { - var txt = commatize(n, 1000) - bias = bias || 1 - n = n || 0 - if (n < 30) { return["quiet", n + " v."] } - if (n < 200) { return ["quiet", txt + " v."] } - else if (n < 500) { return ["quiet", txt + " v."] } - else if (n < 1000) { return ["old", txt + " v."] } - else if (n < 5000) { return ["med", txt + " kv."] } - else if (no_bold || n < 10000) { return ["recent", txt + " kv."] } - else { return ["new", txt + " kv."] } -} -export function hush_threads (n, bias, no_bold) { - var txt = commatize(n, 1000) - bias = bias || 1 - n = n || 0 - if (n < 10) { return["quiet", n + " t."] } - else if (n < 25) { return ["old", txt + " t."] } - else if (n < 50) { return ["med", txt + " t."] } - else if (no_bold || n < 100) { return ["recent", txt + " t."] } - else { return ["new", txt + " t."] } -} -export function hush_size (n, bias, no_bold) { - var txt = commatize(Math.round(n / 1024)) - bias = 1 || bias - n = n || 0 - if (! n) { return ['', ''] } - if (n < 1000) { - return ["quiet", n + " b."] - } - if (n < 1000000) { - return ["quiet", txt + " kb."] - } - else if (n < (20000000/bias)) { - return ["quiet", txt + " mb."] - } - else if (n < (50000000/bias)) { - return ["old", txt + " mb."] - } - else if (n < (80000000/bias)) { - return ["med", txt + " mb."] - } - else if (no_bold || n < (170000000/bias)) { - return ["recent", txt + " mb."] - } - else { - return ["new", txt + " mb."] - } -} -export function hush_null (n, unit, no_bold) { - var s = unit ? n + " " + unit + "." : n - if (n < 3) { - return ["quiet", s] - } - else if (n < 6) { - return ["older", s] - } - else if (n < 10) { - return ["old", s] - } - else if (n < 16) { - return ["med", s] - } - else if (no_bold || n < 21) { - return ["recent", s] - } - else { - return ["new", s] - } -} -export function get_age (t) { - var age = Math.abs(+Date.now() - new Date(t))/1000 - var r = Math.floor - var m - if (age < 5) { return "now" } - if (age < 60) { return r(age) + "s" } - age /= 60 - if (age < 60) { return r(age) + "m" } - m = r(age % 60) - age /= 60 - if (m > 0 && age < 2) { return r(age) + "h" + m + "m" } - if (age < 24) { return r(age) + "h" } - age /= 24 - if (age < 7) { return r(age) + "d" } - age /= 7 - if (age < 12) { return r(age) + "w" } - age /= 4 - if (age < 12) { return r(age) + "m" } - age /= 12 - return r(age) + "y" -} -export function courtesy_s (n, s) { return n == 1 ? "" : (s || "s") } diff --git a/app/client/util/math.js b/app/client/util/math.js new file mode 100644 index 0000000..253bacd --- /dev/null +++ b/app/client/util/math.js @@ -0,0 +1,52 @@ +export function mod(n,m){ return n-(m * Math.floor(n/m)) } +export function clamp(n,a,b) { return n<a?a:n<b?n:b } +export function norm(n,a,b) { return (n-a) / (b-a) } +export function lerp(n,a,b) { return (b-a)*n+a } +export function mix(n,a,b) { return a*(1-n)+b*n } +export function randint(n) { return Math.floor(Math.random()*n) } +export function randrange(a,b){ return Math.random() * (b-a) + a } +export function randsign(){ return Math.random() >= 0.5 ? -1 : 1 } +export function choice (a){ return a[ Math.floor(Math.random() * a.length) ] } +export function lerp(n,a,b){ return (b-a)*n+a } +export function angle(x0,y0,x1,y1){ return Math.atan2(y1-y0,x1-x0) } +export function dist(x0,y0,x1,y1){ return Math.sqrt(Math.pow(x1-x0,2)+Math.pow(y1-y0,2)) } +export function xor(a,b){ a=!!a; b=!!b; return (a||b) && !(a&&b) } +export function quantize(a,b){ return Math.floor(a/b)*b } +export function shuffle(a){ + for (var i = a.length; i > 0; i--){ + var r = randint(i) + var swap = a[i-1] + a[i-1] = a[r] + a[r] = swap + } + return a +} +// returns a gaussian random function with the given mean and stdev. +export function gaussian(mean, stdev) { + let y2; + let use_last = false; + return () => { + let y1; + if (use_last) { + y1 = y2; + use_last = false; + } + else { + let x1, x2, w; + do { + x1 = 2.0 * Math.random() - 1.0; + x2 = 2.0 * Math.random() - 1.0; + w = x1 * x1 + x2 * x2; + } while( w >= 1.0); + w = Math.sqrt((-2.0 * Math.log(w))/w); + y1 = x1 * w; + y2 = x2 * w; + use_last = true; + } + + let retval = mean + stdev * y1; + if (retval > 0) + return retval; + return -retval; + } +} |
