Prettified all files.
This commit is contained in:
+40
-48
@@ -61,31 +61,31 @@ export function sample(population, k) {
|
||||
*/
|
||||
|
||||
if (!Array.isArray(population))
|
||||
throw new TypeError("Population must be an array.")
|
||||
throw new TypeError('Population must be an array.')
|
||||
let n = population.length
|
||||
if (k < 0 || k > n)
|
||||
throw new RangeError("Sample larger than population or is negative")
|
||||
throw new RangeError('Sample larger than population or is negative')
|
||||
|
||||
let result = new Array(k)
|
||||
let setsize = 21 // size of a small set minus size of an empty list
|
||||
let setsize = 21 // size of a small set minus size of an empty list
|
||||
|
||||
if (k > 5)
|
||||
setsize += Math.pow(4, Math.ceil(Math.log(k * 3, 4)))
|
||||
if (k > 5) setsize += Math.pow(4, Math.ceil(Math.log(k * 3, 4)))
|
||||
|
||||
if (n <= setsize) {
|
||||
// An n-length list is smaller than a k-length set
|
||||
let pool = population.slice()
|
||||
for (let i = 0; i < k; i++) { // inletiant: non-selected at [0,n-i)
|
||||
let j = Math.random() * (n - i) | 0
|
||||
for (let i = 0; i < k; i++) {
|
||||
// inletiant: non-selected at [0,n-i)
|
||||
let j = (Math.random() * (n - i)) | 0
|
||||
result[i] = pool[j]
|
||||
pool[j] = pool[n - i - 1] // move non-selected item into vacancy
|
||||
pool[j] = pool[n - i - 1] // move non-selected item into vacancy
|
||||
}
|
||||
} else {
|
||||
let selected = new Set()
|
||||
for (let i = 0; i < k; i++) {
|
||||
let j = Math.random() * (n - i) | 0
|
||||
let j = (Math.random() * (n - i)) | 0
|
||||
while (selected.has(j)) {
|
||||
j = Math.random() * (n - i) | 0
|
||||
j = (Math.random() * (n - i)) | 0
|
||||
}
|
||||
selected.add(j)
|
||||
result[i] = population[j]
|
||||
@@ -95,7 +95,6 @@ export function sample(population, k) {
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
// Returns a function, that, as long as it continues to be invoked, will not
|
||||
// be triggered. The function will be called after it stops being called for
|
||||
// N milliseconds. If `immediate` is passed, trigger the function on the
|
||||
@@ -103,10 +102,10 @@ export function sample(population, k) {
|
||||
// Taken from: https://davidwalsh.name/essential-javascript-functions
|
||||
export function debounce(func, wait, immediate) {
|
||||
let timeout
|
||||
return function () {
|
||||
return function() {
|
||||
let context = this,
|
||||
args = arguments
|
||||
let later = function () {
|
||||
let later = function() {
|
||||
timeout = null
|
||||
if (!immediate) func.apply(context, args)
|
||||
}
|
||||
@@ -135,7 +134,6 @@ export function randomFloat(min = 0.0, max = 1.0) {
|
||||
}
|
||||
|
||||
export class Dates {
|
||||
|
||||
static create(fullYear, month, day) {
|
||||
return new Date(Date.UTC(fullYear, month, day))
|
||||
}
|
||||
@@ -145,7 +143,9 @@ export class Dates {
|
||||
}
|
||||
|
||||
static startYearRange(date) {
|
||||
return new Date(Date.UTC(date.getFullYear() - 1, 11, 31, 23, 59, 59, 999))
|
||||
return new Date(
|
||||
Date.UTC(date.getFullYear() - 1, 11, 31, 23, 59, 59, 999)
|
||||
)
|
||||
}
|
||||
|
||||
static endYearRange(date) {
|
||||
@@ -433,7 +433,6 @@ export class Points {
|
||||
* @class Sets
|
||||
*/
|
||||
export class Sets {
|
||||
|
||||
/**
|
||||
* Returns the intersection of all sets
|
||||
* https://stackoverflow.com/questions/31930894/javascript-set-data-structure-intersect
|
||||
@@ -444,12 +443,10 @@ export class Sets {
|
||||
*/
|
||||
static intersect(...sets) {
|
||||
if (!sets.length) return new Set()
|
||||
const i = sets.reduce((m, s, i) => s.size < sets[m].size ? i : m, 0)
|
||||
const i = sets.reduce((m, s, i) => (s.size < sets[m].size ? i : m), 0)
|
||||
const [smallest] = sets.splice(i, 1)
|
||||
const res = new Set()
|
||||
for (let val of smallest)
|
||||
if (sets.every(s => s.has(val)))
|
||||
res.add(val)
|
||||
for (let val of smallest) if (sets.every(s => s.has(val))) res.add(val)
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -497,7 +494,6 @@ export class Sets {
|
||||
/** Static methods to compute angles.
|
||||
*/
|
||||
export class Angle {
|
||||
|
||||
static normalize(angle) {
|
||||
let TAU = Math.PI * 2.0
|
||||
while (angle > Math.PI) {
|
||||
@@ -544,11 +540,11 @@ export class Angle {
|
||||
}
|
||||
|
||||
static degree2radian(degree) {
|
||||
return Math.PI * degree / 180.0
|
||||
return (Math.PI * degree) / 180.0
|
||||
}
|
||||
|
||||
static radian2degree(rad) {
|
||||
return 180.0 / Math.PI * rad
|
||||
return (180.0 / Math.PI) * rad
|
||||
}
|
||||
}
|
||||
|
||||
@@ -742,10 +738,9 @@ export class Polygon {
|
||||
if (
|
||||
verty[i] > testy != verty[j] > testy &&
|
||||
testx <
|
||||
(vertx[j] - vertx[i]) *
|
||||
(testy - verty[i]) /
|
||||
(verty[j] - verty[i]) +
|
||||
vertx[i]
|
||||
((vertx[j] - vertx[i]) * (testy - verty[i])) /
|
||||
(verty[j] - verty[i]) +
|
||||
vertx[i]
|
||||
)
|
||||
c = !c
|
||||
}
|
||||
@@ -909,13 +904,10 @@ export class Polygon {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Util functions to deal with DOMRects.
|
||||
*/
|
||||
export class Rect {
|
||||
|
||||
|
||||
/**
|
||||
* Test if a given point is contained by the provided Rect.
|
||||
*
|
||||
@@ -926,12 +918,14 @@ export class Rect {
|
||||
* @memberof Rect
|
||||
*/
|
||||
static contains(rect, point) {
|
||||
return (point.x > rect.left &&
|
||||
point.x < rect.x + rect.right
|
||||
&& point.y > rect.top && point.y < rect.bottom)
|
||||
return (
|
||||
point.x > rect.left &&
|
||||
point.x < rect.x + rect.right &&
|
||||
point.y > rect.top &&
|
||||
point.y < rect.bottom
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*Returns the position of an rect as point object.
|
||||
*
|
||||
@@ -948,7 +942,6 @@ export class Rect {
|
||||
/** String utility functions */
|
||||
|
||||
export class Strings {
|
||||
|
||||
static toUpperCaseFirstChar(str) {
|
||||
return str.substr(0, 1).toUpperCase() + str.substr(1)
|
||||
}
|
||||
@@ -958,19 +951,22 @@ export class Strings {
|
||||
}
|
||||
|
||||
static toUpperCaseEachWord(str, delim = ' ') {
|
||||
return str.split(delim).map((v) => v.toUpperCaseFirstChar()).join(delim)
|
||||
return str
|
||||
.split(delim)
|
||||
.map(v => v.toUpperCaseFirstChar())
|
||||
.join(delim)
|
||||
}
|
||||
|
||||
static toLowerCaseEachWord(str, delim = ' ') {
|
||||
return str.split(delim).map((v) => v.toLowerCaseFirstChar()).join(delim)
|
||||
return str
|
||||
.split(delim)
|
||||
.map(v => v.toLowerCaseFirstChar())
|
||||
.join(delim)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export class LowPassFilter {
|
||||
|
||||
constructor(smoothing = 0.5, bufferMaxSize=10) {
|
||||
constructor(smoothing = 0.5, bufferMaxSize = 10) {
|
||||
this.smoothing = smoothing // must be smaller than 1
|
||||
this.buffer = [] // FIFO queue
|
||||
this.bufferMaxSize = bufferMaxSize
|
||||
@@ -978,7 +974,7 @@ export class LowPassFilter {
|
||||
|
||||
/**
|
||||
* Setup buffer with array of values
|
||||
*
|
||||
*
|
||||
* @param {array} values
|
||||
* @returns {array}
|
||||
* @access public
|
||||
@@ -1007,9 +1003,8 @@ export class LowPassFilter {
|
||||
* @access private
|
||||
*/
|
||||
__push(value) {
|
||||
let removed = (this.buffer.length === this.bufferMaxSize)
|
||||
? this.buffer.shift()
|
||||
: 0
|
||||
let removed =
|
||||
this.buffer.length === this.bufferMaxSize ? this.buffer.shift() : 0
|
||||
|
||||
this.buffer.push(value)
|
||||
return removed
|
||||
@@ -1023,7 +1018,6 @@ export class LowPassFilter {
|
||||
* @access public
|
||||
*/
|
||||
next(nextValue) {
|
||||
|
||||
// push new value to the end, and remove oldest one
|
||||
let removed = this.__push(nextValue)
|
||||
// smooth value using all values from buffer
|
||||
@@ -1052,5 +1046,3 @@ export class LowPassFilter {
|
||||
return values
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user