1.0.0 • Published 2 years ago

array-compact-map v1.0.0

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
2 years ago

array-compact-map

npm version npm downloads

array-compact-map JS/TS library that expose a transformation function (eq. to Array.map) The returns array containing non-undefined results of calling the given transformation with each element of this array.

Use this method to receive an array of non-undefined values when your transformation produces an optional value.

Installation

npm i array-compact-map

Usage

import "array-compact-map"; // you must import the framework on your startup file

const strings: string[] = ["10", "500", "hello", "1000", "world"];
const res: number[] = strings.compactMap((str) => {
    const n = Number.parseInt(str);
    if(!isNaN(n)) {
        return n;
    }
});
console.log(res); // [ 10, 500, 1000 ]