@aryth/rand
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -55,2 +55,44 @@ 'use strict'; | ||
const swap = function (i, j) { | ||
const temp = this[i]; | ||
this[i] = this[j]; | ||
return this[j] = temp; | ||
}; | ||
/** | ||
* Fisher–Yates shuffle, a.k.a Knuth shuffle | ||
* @param {Array} ve | ||
* @param {number} [size] - if omitted, size will be keys.length | ||
* @returns {Array} mutated array | ||
*/ | ||
const shuffle = function (ve, size) { | ||
let l = ve.length; | ||
const lo = comparer.max(0, l - (size !== null && size !== void 0 ? size : l)); | ||
for (--l; l >= lo; l--) swap.call(ve, l, rand(l)); | ||
return lo ? (ve.splice(0, lo), ve) : ve; | ||
}; | ||
/** | ||
* | ||
* Object keys can be set via 'this.keys' | ||
* Default keys are Object.keys(o), the enumerable list of o's keys. | ||
* @param {Object} o | ||
* @param {number} [size] - if omitted, size will be keys.length | ||
* @returns {Object} new object | ||
*/ | ||
const shuffleObject = function (o, size) { | ||
const keys = (this === null || this === void 0 ? void 0 : this.keys) || Object.keys(o); | ||
let l = keys.length, | ||
k; | ||
const lo = comparer.max(0, l - (size !== null && size !== void 0 ? size : l)), | ||
rs = {}; | ||
for (--l; l >= lo; l--) rs[k = swap.call(keys, rand(l), l)] = o[k]; | ||
return rs; | ||
}; | ||
function indexShuffler(ar) { | ||
@@ -81,24 +123,2 @@ let length = this.length || ar.length; | ||
const swap = function (i, j) { | ||
const temp = this[i]; | ||
this[i] = this[j]; | ||
return this[j] = temp; | ||
}; | ||
/** | ||
* Fisher–Yates shuffle, a.k.a Knuth shuffle | ||
* @param {Array} ar | ||
* @param {number} [size] | ||
*/ | ||
const knuthShuffle = function (ar, size) { | ||
let l = ar.length; | ||
const hi = size > l ? l : size || l, | ||
tail = l - hi; | ||
for (--l; l >= tail; l--) swap.call(ar, l, rand(l)); | ||
return tail ? (ar.splice(hi, tail), ar) : ar; | ||
}; | ||
exports.Shuffler = Shuffler; | ||
@@ -114,2 +134,3 @@ exports.flop = flop; | ||
exports.randLongStr = randLongStr; | ||
exports.shuffle = knuthShuffle; | ||
exports.shuffle = shuffle; | ||
exports.shuffleObject = shuffleObject; |
@@ -1,2 +0,2 @@ | ||
import { min } from '@aryth/comparer'; | ||
import { max, min } from '@aryth/comparer'; | ||
@@ -51,2 +51,44 @@ const { | ||
const swap = function (i, j) { | ||
const temp = this[i]; | ||
this[i] = this[j]; | ||
return this[j] = temp; | ||
}; | ||
/** | ||
* Fisher–Yates shuffle, a.k.a Knuth shuffle | ||
* @param {Array} ve | ||
* @param {number} [size] - if omitted, size will be keys.length | ||
* @returns {Array} mutated array | ||
*/ | ||
const shuffle = function (ve, size) { | ||
let l = ve.length; | ||
const lo = max(0, l - (size !== null && size !== void 0 ? size : l)); | ||
for (--l; l >= lo; l--) swap.call(ve, l, rand(l)); | ||
return lo ? (ve.splice(0, lo), ve) : ve; | ||
}; | ||
/** | ||
* | ||
* Object keys can be set via 'this.keys' | ||
* Default keys are Object.keys(o), the enumerable list of o's keys. | ||
* @param {Object} o | ||
* @param {number} [size] - if omitted, size will be keys.length | ||
* @returns {Object} new object | ||
*/ | ||
const shuffleObject = function (o, size) { | ||
const keys = (this === null || this === void 0 ? void 0 : this.keys) || Object.keys(o); | ||
let l = keys.length, | ||
k; | ||
const lo = max(0, l - (size !== null && size !== void 0 ? size : l)), | ||
rs = {}; | ||
for (--l; l >= lo; l--) rs[k = swap.call(keys, rand(l), l)] = o[k]; | ||
return rs; | ||
}; | ||
function indexShuffler(ar) { | ||
@@ -77,24 +119,2 @@ let length = this.length || ar.length; | ||
const swap = function (i, j) { | ||
const temp = this[i]; | ||
this[i] = this[j]; | ||
return this[j] = temp; | ||
}; | ||
/** | ||
* Fisher–Yates shuffle, a.k.a Knuth shuffle | ||
* @param {Array} ar | ||
* @param {number} [size] | ||
*/ | ||
const knuthShuffle = function (ar, size) { | ||
let l = ar.length; | ||
const hi = size > l ? l : size || l, | ||
tail = l - hi; | ||
for (--l; l >= tail; l--) swap.call(ar, l, rand(l)); | ||
return tail ? (ar.splice(hi, tail), ar) : ar; | ||
}; | ||
export { Shuffler, flop, flopEntry, flopIndex, flopKey, flopValue, rand, randInt, randIntBetw, randLongStr, knuthShuffle as shuffle }; | ||
export { Shuffler, flop, flopEntry, flopIndex, flopKey, flopValue, rand, randInt, randIntBetw, randLongStr, shuffle, shuffleObject }; |
{ | ||
"name": "@aryth/rand", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "A math util library", | ||
@@ -18,3 +18,3 @@ "main": "dist/index.cjs.js", | ||
"dependencies": { | ||
"@aryth/comparer": "^0.0.11" | ||
"@aryth/comparer": "^0.0.12" | ||
}, | ||
@@ -36,3 +36,3 @@ "repository": { | ||
"homepage": "https://github.com/hoyeungw/aryth#readme", | ||
"gitHead": "ee03239bb60f21e7cbd3371bb9d9143bb8940284" | ||
"gitHead": "2c03e9e9a49eb2eff77e5550acbd1d75074cd054" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9531
203
+ Added@aryth/comparer@0.0.12(transitive)
- Removed@aryth/comparer@0.0.11(transitive)
Updated@aryth/comparer@^0.0.12