npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lucky-item

v2.0.2

Published

Randomly select some items from an array. Also can random based on weights.

Downloads

19

Readme

lucky-item

Build Status Coverage Status

Randomly select some items from an array. Also can random based on weights.

npm install lucky-item --save

Eglish | 中文简体

Usage

const lucky = require('lucky-item').default;

const arr = [
    { id: 1, weight: 10 },
    { id: 2, weight: 20 },
    { id: 3, weight: 40 },
    { id: 4, weight: 80 },
    { id: 5, weight: 160 },
    { id: 6, weight: 320 },
    { id: 7, weight: 640 },
    { id: 8, weight: 1280 },
    { id: 9, weight: 2560 },
    { id: 10, weight: 5120 },
];

lucky.indexs(arr, 3);
// [ 3, 8, 5 ]

lucky.index(arr);
// 4

lucky.items(arr, 3);
// [
//   { id: 7, weight: 640 },
//   { id: 8, weight: 1280 },
//   { id: 2, weight: 20 }
// ]

lucky.item(arr);
// { id: 10, weight: 5120 }

lucky.indexsBy(arr, 'weight', 3);
// [ 9, 8, 3 ]

lucky.indexBy(arr, 'weight');
// 9

lucky.itemsBy(arr, 'weight', 3);
// [
//   { id: 10, weight: 5120 },
//   { id: 9, weight: 2560 },
//   { id: 6, weight: 320 }
// ]

lucky.itemBy(arr, 'weight');
// { id: 10, weight: 5120 }

If you want lucky indexs or items can repeated:

const arr = [
    { id: 1, weight: 10 },
    { id: 2, weight: 20 },
    { id: 3, weight: 40 },
    { id: 4, weight: 80 },
    { id: 5, weight: 160 },
    { id: 6, weight: 320 },
    { id: 7, weight: 640 },
    { id: 8, weight: 1280 },
    { id: 9, weight: 2560 },
    { id: 10, weight: 5120 },
];

// Method 1:
const lucky = require('lucky-item').default;

lucky.itemsBy(arr, 'weight', 3, { unique: false });
// [
//   { id: 10, weight: 5120 },
//   { id: 9, weight: 2560 },
//   { id: 10, weight: 5120 }
// ]

// Method 2:
const LuckyItem = require('lucky-item').LuckyItem;
const repeatableLucky = new LuckyItem({ unique: false });

repeatableLucky.itemsBy(arr, 'weight', 3);
// [
//   { id: 10, weight: 5120 },
//   { id: 4, weight: 80 },
//   { id: 10, weight: 5120 }
// ]

API

indexs

indexs<T>(arr: T[], count: number, options?: Options): number[];

Randomly select some indexs from the array.

@param arr The array for selection.

@param count The number of indexs you want to select.

@param options (optional) For more details, please see the following description of the interface.

@returns Returns the array of lucky indexs.

index

index<T>(arr: T[], options?: Options): number;

Randomly select an index from the array.

@param arr The array for selection.

@param options (optional) For more details, please see the following description of the interface.

@returns Returns the lucky index.

items

items<T>(arr: T[], count: number, options?: Options): T[];

Randomly select some items from the array.

@param arr The array for selection.

@param count The number of items you want to select.

@param options (optional) For more details, please see the following description of the interface.

@returns Returns the array of lucky items

item

item<T>(arr: T[], options?: Options): T;

Randomly select an item from the array.

@param arr The array for selection.

@param options (optional) For more details, please see the following description of the interface.

@returns Returns the lucky item.

indexsBy

indexsBy<T>(arr: T[], weights: string | GetWeightsFunc<T>, count: number, options?: Options): number[];

Randomly select some indexs from the array based on weights.

@param arr The array for selection.

@param weights The field name of weights or a function that returns weights.

@param count The number of indexs you want to select.

@param options (optional) For more details, please see the following description of the interface.

@returns Returns the array of lucky indexs.

indexBy

indexBy<T>(arr: T[], weights: string | GetWeightsFunc<T>, options?: Options): number;

Randomly select an index from the array based on weights.

@param arr The array for selection.

@param weights The field name of weights or a function that returns weights.

@param options (optional) For more details, please see the following description of the interface.

@returns Returns the lucky index.

itemsBy

itemsBy<T>(arr: T[], weights: string | GetWeightsFunc<T>, count: number, options?: Options): T[];

Randomly select some items from the array based on weights.

@param arr The array for selection.

@param weights The field name of weights or a function that returns weights.

@param count The number of items you want to select.

@param options (optional) For more details, please see the following description of the interface.

@returns Returns the array of lucky items.

itemBy

itemBy<T>(arr: T[], weights: string | GetWeightsFunc<T>, options?: Options): T;

Randomly select an item from the array based on weights.

@param arr The array for selection.

@param weights The field name of weights or a function that returns weights.

@param options (optional) For more details, please see the following description of the interface.

@returns Returns the lucky item.

Interface

Options

declare interface Options {
    unique?: boolean;
    random?: RandomFunc;
}

unique:

Type: boolean

Default: true

If unique is false, lucky indexs or items can be repeated.

random:

Type: Function

Default:

function _random(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

A function that return a number between min and max.

(min: number, max: number): number

GetWeightsFunc

declare interface GetWeightsFunc<T> {
    (a: T): number;
}

A function that return weights. This function will be called when we need the weights of an item. This function should return a non-negative number.

License

MIT © Cody Tseng