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

mapfun

v0.9.20

Published

A function that applies a mapping function to an infinite number of input elements, with options to skip certain elements and selectively apply the mapping to keys and/or values of objects.

Downloads

208

Readme

📝 Javascript provides a built-in Math module with various functions.

⚠️However, there is room for improvement in terms of efficiency. For instance, the Math.sqrt(x) function can calculate the square root of a number x, but it has limitations such as the inability to accept multiple parameters and the inability to map the function to different data types like Arrays and Objects.

💡 In zikojs, I have addressed these limitations, providing a more versatile and efficient solution using the mapfun utility which is a function that applies a mapping function to an infinite number of input elements, with options to skip certain elements and selectively apply the mapping to keys and/or values of objects.

💡 The mapfun function has been developed in JavaScript and Python, offering the same functionality in different programming languages.

Install

npm install mapfun

Import

  • common js :

const {mapfun} = require("mapfun");
  • es module

import {mapfun} from "mapfun" 

unpkg

<script src="https://unpkg.com/mapfun@latest/dist/mapfun.js"></script>

Syntax

mapfun(fun,{skip,key,value},...X)

Arguments

  • fun : The mapping function that will be applied to each element in the input elements.
  • ...X :The elements to be mapped .
  • { skip , key , value } : Object with three optional properties :
    • skip : specifies the elements to skip during the mapping process.
    • key : boolean flag indicating whether to apply the mapping function to the keys of objects or not. The default value is false
    • value : boolean flag indicating whether to apply the mapping function to the values of objects or not. The default value is true

Examples

console.log(
  mapfun(n => n + 2,{}, 1,"a", [1, 2, 3], true, {
    a: 2,
    b: 3,
    c: { d: 3, e: [1, 3, 4] },
  })
);
/* Expected log
[
  3,
  "a2",
  [3, 4, 5],
  3,
  {
    a: 4,
    b: 5,
    c: {
      d: 5,
      e: [3, 5, 6],
    },
  },
];
*/
const PI=Math.PI
const sin=(...args)=>mapfun(Math.sin,{},...args)
console.log(sin(0,PI/4,[0,PI/6,PI/4,{x:PI/8,y:PI/12}]))
/* Expected log
[
  0,
  0.7071067811865475,
  [
    0,
    0.49999999999999994,
    0.7071067811865475,
    {
      x: 0.3826834323650898,
      y: 0.25881904510252074,
    },
  ],
];
*/

Advanced Examples

 const A=new Map([["a",1],["b",2]]);
 // Map { 'a' => 1, 'b' => 2 }
 console.log(mapfun(n=>n+1,{},A));
 /* Expected log 
   Map { 'a' => 2, 'b' => 3 }
 */
 console.log(mapfun(n=>n+1,{key:true},A));
 /* Expected log 
   Map { 'a1' => 2, 'b1' => 3 }
 */
 console.log(mapfun(n=>n+1,{value:false},A));
 /* Expected log 
   Map { 'a' => 1, 'b' => 2 }
 */
 console.log(mapfun(n=>n+1,{key:true,value:false},A));
 /* Expected log 
   Map { 'a1' => 1, 'b1' => 2 }
 */
const pow=(...x)=>{
n=x.pop();
   return mapfun(a=>Math.pow(a,n),{},...x)
}
console.log(pow(1,2,{y:2},3))

Supported datatypes

|Datatype|Support| |-|-| |Number|✅| |String|✅| |Boolean|✅| |Null|✅| |NaN|✅| |Undefined|✅| |BigInt|✅| |Array|✅| |Object|✅| |Map|✅| |WeakMap|❌| |Set|✅| |WeakSet|❌| |ArrayBuffer|✅| |Symbol|❌|

⚠️

  • mapfun with ArrayBuffer returns Array not ArrayBuffer .
  • BigInt and Number are not interchangeable, and you cannot directly perform arithmetic operations between them .

Alternatives

Python

License

This projet is licensed under the terms of MIT License .