1.0.4 • Published 6 years ago

jsmp-infra-milanoleg v1.0.4

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

logo

Educational package

Curated collection of useful JavaScript snippets that you can understand in 20 seconds or less.

  • Use Ctrl + F or command + F to search for a snippet.

Installation

You can find a package details npm.

# Install with npm

npm install jsmp-infra-milanoleg

Usage examples

// ES Modules

import { compact } from  'jsmp-infra-milanoleg';

compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]);

To import snippets with Node:

const { compact } = require('jsmp-infra-milanoleg');

compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]);

Table of Contents

📚 Array

📜 String

📚 Array

concat

Concatenate some arrays into one array.

Uses _.concat() to concatenate arrays.

const firstArray = [1, null, 'Hello', 1];
const secondArray = [false, 12, 'someString'];
const  concat = concat(firstArray, secondArray);
concat([1, null, 'Hello', 1], [false, 12, 'someString']); // [1, null, 'Hello', 1, false, 12, 'someString']

⬆ Back to top

compact

Removes falsey values from an array.

Uses _.compact() to filter out falsey values (false, null, 0, "", undefined, and NaN).

const  compact = arr  =>  _.compact([1, null, 'Hello', 1]);
compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]); // [ 1, 2, 3, 'a', 's', 34 ]

⬆ Back to top

unique

Returns new array which contains only unique elements.

Create a new array from the given array.

const  unique = unique([1, null, 'Hello', 1]);
unique([1, null, 'Hello', 1]); // [1, null, 'Hello']

⬆ Back to top

📜 String

toCamelCase

Converts a string to camelcase.

Break the string into words and combine them capitalizing the first letter of each word, using a regexp.

const toCamelCase = str => {
  let s =
    str &&
    str
      .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
      .map(x => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
      .join('');
  return s.slice(0, 1).toLowerCase() + s.slice(1);
};
toCamelCase('some_database_field_name'); // 'someDatabaseFieldName'
toCamelCase('Some label that needs to be camelized'); // 'someLabelThatNeedsToBeCamelized'
toCamelCase('some-javascript-property'); // 'someJavascriptProperty'
toCamelCase('some-mixed_string with spaces_underscores-and-hyphens'); // 'someMixedStringWithSpacesUnderscoresAndHyphens'

⬆ Back to top

toKebabCase

Converts a string to kebab case.

Break the string into words and combine them adding - as a separator, using a regexp.

const toKebabCase = str =>
  str &&
  str
    .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
    .map(x => x.toLowerCase())
    .join('-');
toKebabCase('camelCase'); // 'camel-case'
toKebabCase('some text'); // 'some-text'
toKebabCase('some-mixed_string With spaces_underscores-and-hyphens'); // 'some-mixed-string-with-spaces-underscores-and-hyphens'
toKebabCase('AllThe-small Things'); // "all-the-small-things"
toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML'); // "i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html"

⬆ Back to top

toSnakeCase

Truncates a string up to a specified length.

Converts a string to snake case.

Break the string into words and combine them adding _ as a separator, using a regexp.

const toSnakeCase = str =>
  str &&
  str
    .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
    .map(x => x.toLowerCase())
    .join('_');
toSnakeCase('camelCase'); // 'camel_case'
toSnakeCase('some text'); // 'some_text'
toSnakeCase('some-mixed_string With spaces_underscores-and-hyphens'); // 'some_mixed_string_with_spaces_underscores_and_hyphens'
toSnakeCase('AllThe-small Things'); // "all_the_smal_things"
toSnakeCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML'); // "i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_some_xml_and_html"

⬆ Back to top

Credits

Icons made by Smashicons from www.flaticon.com is licensed by CC 3.0 BY.

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago