Coder Social home page Coder Social logo

arrify's Introduction

arrify

Convert a value to an array

If you simply want to accept both a single value and multiple values, you can use [singleValueOrArray].flat() instead.

Install

npm install arrify

Usage

import arrify from 'arrify';

arrify('๐Ÿฆ„');
//=> ['๐Ÿฆ„']

arrify(['๐Ÿฆ„']);
//=> ['๐Ÿฆ„']

arrify(new Set(['๐Ÿฆ„']));
//=> ['๐Ÿฆ„']

arrify(null);
//=> []

arrify(undefined);
//=> []

Specifying null or undefined results in an empty array.


Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

arrify's People

Contributors

ajafff avatar arthurvr avatar bendingbender avatar coreyfarrell avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

arrify's Issues

Invalid @@iterator method will produce an error

If an iterable's @@iterator method does not return an iterator object, then it is a non-well-formed iterable.
Just like this:

import arrify from "arrify";

let iterable = {  
  0: "a",  
  1: "b",  
  2: "c",  
  length: 3,  
  [Symbol.iterator]: () => 1,  
};  
console.log(arrify(iterable));

You will get an error:

file:///D:/05-personal/node/nodejs-debugging/node_modules/arrify/index.js:15
                return [...value];
                           ^

TypeError: Result of the Symbol.iterator method is not an object
    at arrify (file:///D:/05-personal/node/nodejs-debugging/node_modules/arrify/index.js:15:14)
    at file:///D:/05-personal/node/nodejs-debugging/src/index.js:50:13
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)
error Command failed with exit code 1.

Doesn't work with array-like objects

This sounded interesting but my main use case would be transforming results from document.querySelector(All) to arrays, and that doesn't work (meaning that they end up in a single-element array, always)

Can this be modified to work with array-like objects?

Unable to use import syntax in TypeScript

I am unable to import this package through TypeScript.
When I try, it gives me the following error

Module '"path/to/project/node_modules/arrify/index"' can only be default-imported using the 'esModuleInterop' flagts(1259)
index.d.ts(38, 1): This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

type definitions don't work for tuple types

declare let tuple: [number, string];

const arrified = arrify(tuple);
arrified.push(1);
arrified[0] = 'foo'

Since tuples are arrays, they are returned as is. The type declarations however widen the type to a regular (mutable) array type. Both mutations above should not be allowed.

Support for multiple arguments

Would it make sense to include support for multiple arguments?

var arrify = require('arrify');
arrify('unicorn', 'unicorn');

and if so, would it then best return:

//=> ['unicorn', 'unicorn']

or

//=> [['unicorn'], ['unicorn']]

or is the module intentionally strict to only support one argument?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.