Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
choice function returns random item from an array
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisweb committed Aug 27, 2020
1 parent 88abcc5 commit fd1d678
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 214 deletions.
75 changes: 53 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Javascript utilities belt for very specific tasks.

main goal(s):

* console log wrapper for colored log messages
* utilities belt: javascript collection of useful functions that are useful for specific situations, so a lot more specific than underscore
* should work in the browser as well in nodejs
Expand All @@ -35,7 +36,7 @@ update npm to latest version

install the dependencies

`npm install`
`npm i`

to lint the typescript files

Expand All @@ -49,10 +50,6 @@ to build the distributions (es6 module (esm.js) and the UMD version)

`npm run build`

or by using yarn

`yarn build`

## development

in devlopment you can use watch to rebuild every time you edit a typescript file
Expand All @@ -79,29 +76,23 @@ installed dependencies:

command to create a basic jest.config.js file:

`npx ts-jest config:init yarn ts-jest config:init`
`npx ts-jest config:init yarn ts-jest config:init`

If you use VSCode, consider using the [vscode jest extension](https://github.com/jest-community/vscode-jest)

Besides reading the official documentation, you can check out this quick [introduction to jest](https://flaviocopes.com/jest/) blog post

## linting setup notes

> we plan to deprecate TSLint and focus our efforts instead on improving ESLint’s TypeScript support

Their [blog post](https://medium.com/palantir/tslint-in-2019-1a144c2317a9) announcing that [TSLint](https://github.com/palantir/tslint) will be deprecated in 2019 and you should switch to [ESLint](https://github.com/eslint/eslint) when TSLint is officially deprecated

TSlint will be deprecated [github ticket](https://github.com/palantir/tslint/issues/4534)

## features

### utilities.log

Colored console log messages for the browser and / or nodejs
```

```javascript
utilities.log('foo', 'bar', 'fontColor:red', 'backgroundColor:blue');
```

**Suppported colors**
#### Suppported colors

* red
* green
Expand All @@ -112,49 +103,89 @@ Colored console log messages for the browser and / or nodejs
* white
* black

**logging in floating html div (client)**
#### logging in floating html div (client)

If you are on mobile device and wan't to show the messages in a floating div over the page then set logSpecial to true
```

```javascript
utilities.logSpecial = true
```

**logging in a file (server)**
#### logging in a file (server)

If want every message to logged in file, then enable logSpecial (currently only winston is supported)
```

```javascript
utilities.logSpecial = true
```

**logging in a file (server) but not in the console**
```
#### logging in a file (server) but not in the console

```javascript
utilities.logSpecial = true
utilities.logVerbose = false
```

### removeElements

removes html elements from a sting (and their content) from strings
set second parameter "removeTextBetweenTags" to false to keep the text between the opening and closing html tag

### generateUUID

returns a universally unique identifier

### filterAlphaNumericPlus

filters a string
removes everything that is a not an alpha or numeric character and also optionally the characters that got specified as second argument

### decodeUri

decode uri

### encodeUri

encode uri

### arrayRemove

remove an something at an unknown index from an array

### capitaliseFirstLetter

capitalise first letter of a string

### getUrlParameters

get url parameters

### stringContains

does a string contain another string

### getSubstringIndex

get the index of a substring in a string with optional nth time it occurs

### isServer

does the script run on the server

### isClient

does the script run in a client

### replace placholder(s)

first parameter is a string and second parameter an object where the keys are the placeholder that need to get replaced and the value is the replacement (replacement can be a string or number)

### remove the content between two "markers"

### randomInteger

Input are two numbers, output is a random number that is between or equal the two input numbers

### choice

Input is a sequence (an array) of items and the output one random item of the input
4 changes: 2 additions & 2 deletions dist/@types/chrisweb-utilities/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import { generateUUID } from './utilities/uuid';
import { removeElements } from './utilities/html';
import { log } from './utilities/log';
import { sleep } from './utilities/helper';
import { randomInteger } from './utilities/random';
import { randomInteger, choice } from './utilities/random';
declare const version = "1.0.0";
export { version, getTimestamp, isServer, isClient, getUrlParameters, getUrlParameterByName, replaceUrlParameter, decodeUri, encodeUri, removeString, isArray, includes, find, flat, filterAlphaNumericPlus, capitaliseFirstLetter, stringContains, getSubstringIndex, replacePlaceholders, generateUUID, removeElements, log, sleep, randomInteger };
export { version, getTimestamp, isServer, isClient, getUrlParameters, getUrlParameterByName, replaceUrlParameter, decodeUri, encodeUri, removeString, isArray, includes, find, flat, filterAlphaNumericPlus, capitaliseFirstLetter, stringContains, getSubstringIndex, replacePlaceholders, generateUUID, removeElements, log, sleep, randomInteger, choice };
3 changes: 2 additions & 1 deletion dist/@types/chrisweb-utilities/utilities/random.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
* @param max
*/
declare const randomInteger: (min?: number, max?: number) => number;
export { randomInteger };
declare const choice: (sequence?: unknown[]) => unknown;
export { randomInteger, choice };
6 changes: 5 additions & 1 deletion dist/index.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.esm.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit fd1d678

Please sign in to comment.