@vect/object-select
Advanced tools
Comparing version 0.1.15 to 0.1.16
@@ -7,2 +7,3 @@ 'use strict'; | ||
var comparer = require('@aryth/comparer'); | ||
var swap = require('@vect/swap'); | ||
var vectorMapper = require('@vect/vector-mapper'); | ||
@@ -32,3 +33,24 @@ | ||
* | ||
* 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 shuffle = 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 = {}; | ||
while (--l >= lo) rs[k = swap.swap.call(keys, rand.rand(l), l)] = o[k]; | ||
return rs; | ||
}; | ||
/** | ||
* | ||
* @param {Object} o | ||
* @param {*[]} keys | ||
@@ -89,1 +111,2 @@ */ | ||
exports.selectValues = selectValues; | ||
exports.shuffle = shuffle; |
import { rand } from '@aryth/rand'; | ||
import { max, min } from '@aryth/comparer'; | ||
import { swap } from '@vect/swap'; | ||
import { iterate } from '@vect/vector-mapper'; | ||
@@ -27,3 +28,24 @@ | ||
* | ||
* 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 shuffle = 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 = {}; | ||
while (--l >= lo) rs[k = swap.call(keys, rand(l), l)] = o[k]; | ||
return rs; | ||
}; | ||
/** | ||
* | ||
* @param {Object} o | ||
* @param {*[]} keys | ||
@@ -80,2 +102,2 @@ */ | ||
export { leap, lookupKeys, select, selectValues }; | ||
export { leap, lookupKeys, select, selectValues, shuffle }; |
{ | ||
"name": "@vect/object-select", | ||
"version": "0.1.15", | ||
"version": "0.1.16", | ||
"description": "Select from object/array", | ||
@@ -21,3 +21,3 @@ "main": "dist/index.cjs.js", | ||
"@vect/swap": "^0.1.15", | ||
"@vect/vector-mapper": "^0.1.15" | ||
"@vect/vector-mapper": "^0.1.16" | ||
}, | ||
@@ -39,3 +39,3 @@ "repository": { | ||
"homepage": "https://github.com/hoyeungw/vect#readme", | ||
"gitHead": "1b843fe2db07f22e9c8dd7ab2a2b28497c6a964c" | ||
"gitHead": "2f47b07e41f3c477c3cd4df76d8af789faa0c5b9" | ||
} |
8681
170
Updated@vect/vector-mapper@^0.1.16