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

simple-fns

v0.2.1

Published

Manupilate strings of all types to get desired output

Downloads

19

Readme

Manupilate strings of all types to get desired output

npm npm bundle size (minified) licence (MIT)

Usage

import the js-fns library

const { splitText, countOccurance } = require('simple-fns');

// or

const jsFns = require('simple-fns');

splitText

Split a string into an array as well as the count for the length using a specified delimiter

  • options

    1. With option 0 (optional)
    const result = splitText('test test test', ' ');
    
    // result -> { total: 3, list: ['test', 'test', 'test']}
    
    // or
    const result = splitText('test test test', ' ', 0);
    
    // result -> {total: 3,list: ['test', 'test', 'test']}
    1. With option 1
    const result = splitText('test test test', ' ', 1);
    
    // result -> { total: 3}
    
    // or
    const result = splitText('test , test , test', ',', 1);
    
    // result -> { total: 3}
    1. With option 2
    const result = splitText('test test test', ' ', 2);
    
    // result -> {list: ['test', 'test', 'test']}
    
    // or
    const result = splitText('test , test , test', ',', 2);
    
    //result -> {list: ['test', 'test', 'test']}
    // Note: if the delimeter is other than ' ' (space), then all whitespaces will be trimmed from the string before the array is returned.

countOccurance

Count the number of times a character occurs in a string

  • options

    1. With option 0 (optional) -> count Uppercases
    const result1 = countOccurance('Home-work-Space-');
    // result1 -> 2
    
    const result2 = countOccurance('homeworkspace');
    // result2 -> 0
    
    const result3 = countOccurance('homeworkSpace', 0);
    // result3 -> 1
    
    const result4 = countOccurance('homeworkSpace', 0, '');
    // result4 -> 1
    
    const result5 = countOccurance('homeworkSpace', 0, ' ');
    // result5 -> 1
    
    const result6 = countOccurance('homeworkSpace', 0, ',');
    // result6 -> 1
    1. With option 1 -> count Lowercases
    const result1 = countOccurance('Home-work-Space-', 1);
    // result1 -> 11
    
    const result2 = countOccurance('Home work Space', 1);
    // result2 -> 11
    
    const result3 = countOccurance('Home work Space ', 1);
    // result3 -> 11
    
    const result4 = countOccurance('Home, work , Space ', 1);
    // result4 -> 11
    
    const result5 = countOccurance('homeworkspace', 1, ',');
    // result5 -> 13
    1. With option 2 -> digits/ numbers
    const result1 = countOccurance('1ome-2ork-3pace-', 2);
    // result1 -> 3
    
    const result2 = countOccurance('1234', 2);
    // result2 -> 4
    
    const result3 = countOccurance('-1', 2);
    // result3 -> 1
    
    const result4 = countOccurance('1,2,3,4,5', 2);
    // result4 -> 5
    
    const result5 = countOccurance('4  6  . 7', 2);
    // result5 -> 3
    1. With option 3 -> whitespaces in a string
    const result1 = countOccurance('1ome-2ork-3pace-', 3, ' ');
    // result1 -> 0
    
    const result2 = countOccurance('1 2 3 4', 3);
    // result2 -> 3
    
    const result3 = countOccurance(' 1 2 3 4 ', 3);
    // result3 -> 5
    
    const result4 = countOccurance(' 0 ', 3);
    // result4 -> 2
    
    const result5 = countOccurance('Home Work  Space ', 3, ' ');
    // result5 -> 4
    1. With option 4 -> delimiter in a string
    const result1 = countOccurance('1ome-2ork-3pace-', 4, '-');
    // result1 -> 3
    
    const result2 = countOccurance('1ome-2ork-3pace-', 4, '- ');
    // result2 -> 3
    
    const result3 = countOccurance('1ome-2ork-3pace-', 4, ' - ');
    // result3 -> 3
    
    const result4 = countOccurance('1ome/2ork/3pace/', 4, '/');
    // result4 -> 3
    
    const result5 = countOccurance('boy , girl , kid , child', 4, ',');
    // result5 -> 3
    
    const result5 = countOccurance('boy , girl , kid , child', 4, '/');
    // result5 -> 0

    mergeObjects

    Combine two objects in different arrays using their shared identifier

"with 'd' specified, you indicate that you want 'my_id' as a key to be deleted from the returned array objects";

const result1 = mergeObjects(
  [{ id: 1, name: 'Isaac', age: 20 }],
  [{ my_id: 1, address: 'Kampala' }],
  'id',
  'my_id',
  'd'
);

// output -> [{id: 1, name: 'Isaac', age: 20, address: 'Kampala'}]

const result2 = mergeObjects(
  [{ id: 1, name: 'Isaac', age: 20 }],
  [{ my_id: 1, address: 'Kampala' }],
  'id',
  'my_id'
);

// output -> [{id: 1, name: 'Isaac', age: 20, my_id: 1, address: 'Kampala'}]

const result3 = mergeObjects(
  [{ id: 1, name: 'Isaac', age: 20 }],
  [{ id: 1, address: 'Kampala' }],
  'id',
  'id'
);
// output -> [{id: 1, name: 'Isaac', age: 20, address: 'Kampala'}]

const result4 = mergeObjects(
  [{ id: 1, name: 'Isaac', age: 20 }],
  [{ id: 1, address: 'Kampala' }],
  'id',
  'id',
  'd'
);
// output -> [{'Isaac', age: 20, address: 'Kampala'}]

injectDupObj

Inject objects from one array with duplicate id into the main array

const result1 = injectDupObj(
  [
    { id: 1, name: 'john1', age: 30 },
    { id: 2, name: 'john2', age: 30 },
    { id: 3, name: 'john3', age: 30 },
    { id: 4, name: 'john4', age: 30 },
    { id: 5, name: 'john5', age: 30 },
  ],
  [
    { id: 1, user_id: 1, course: 'SST' },
    { id: 2, user_id: 2, course: 'English' },
    { id: 3, user_id: 1, course: 'Science' },
    { id: 8, user_id: 1, course: 'Christianity' },
    { id: 4, user_id: 2, course: 'Luganda' },
    { id: 5, user_id: 3, course: 'Runyankole' },
    { id: 6, user_id: 4, course: 'Swahili' },
    { id: 7, user_id: 7, course: 'Luo' },
  ],
  'id',
  'user_id',
  'courses'
);
// lookupKey is optional, if not give, the inner array will have 'lookup' as its key
// output
[
  {
    id: 1,
    name: 'john1',
    age: 30,
    courses: [
      { id: 1, user_id: 1, course: 'SST' },
      { id: 3, user_id: 1, course: 'Science' },
      { id: 8, user_id: 1, course: 'Christianity' },
    ],
  },
  {
    id: 2,
    name: 'john2',
    age: 30,
    courses: [
      { id: 2, user_id: 2, course: 'English' },
      { id: 4, user_id: 2, course: 'Luganda' },
    ],
  },
  {
    id: 3,
    name: 'john3',
    age: 30,
    courses: [{ id: 5, user_id: 3, course: 'Runyankole' }],
  },
  {
    id: 4,
    name: 'john4',
    age: 30,
    courses: [{ id: 6, user_id: 4, course: 'Swahili' }],
  },
  { id: 5, name: 'john5', age: 30 },
];

Note

  • Added type definitions on version 0.1.0, if you experience any issue, runnpm i [email protected] to install a previous version without types
  • As well raise an issue here