0.3.18 • Published 3 years ago

pauls-pure-functions v0.3.18

Weekly downloads
136
License
-
Repository
-
Last release
3 years ago

📚 pauls-pure-functions 📚

Library of useful functions like underscore/lodash. Very small. No dependencies. A collection of various random useful scripts for data manipulation and modern web development. Please contribute. object_keys_from_array_values

npm package Try it out in CodeSandbox.io: \ https://codesandbox.io/s/pauls-pure-functions-demo-esm-cculn

Why?

Unlike Lodash, these functions are not limited to common data structures, but web development in general - like parsing URLs, making HTTP requests, seeing if the browser screen is Retina or not (that's not pure, but it's useful). My name for this library really needs some thought.

I did not want to constantly re-invent the wheel and search for code snippets. Some of these functions are just like Lodash, but offer extra functionality (or less functionality). It all depends on the individual function.

Currently working on testing and documenting this library. Just finished the build process for browsers!

I'm still adding functions and changing existing ones. Will be renaming all the function and changing the structure. PLEASE NOTE: this project (currently version 0) will not be stable until version 1. Contact me (I don't have a mailing list) to be notified when this becomes stable and complete (probably not for another year). for_each_promise_all

Also check out:

  • numjs{:target="_blank"} - JS version of Python's numpy

Installation:

Node.js ES6 Modules

High five! Shake that tree!

// NOTE: import from "esm" folder:
import { sort_by_rating_and_position } from "pauls-pure-functions/esm/sort_strings"
// or:
import * as string_sort_functions from "pauls-pure-functions/esm/sort_strings"
// or:
import everything from "pauls-pure-functions"

Node.js Common JS

consider converting your project to {"type":"module"}. Or use the handy npm esm package

// NOTE: import from "cjs" folder:
const string_sort_functions = require("pauls-pure-functions/esm/sort_strings")
// or get everything:
const everything = require("pauls-pure-functions")

Browser

this script will create a window.__ variable with a flat list of functions. This is different. In Node, you import/require and you choose what to call each variable. Replace "__" with a specific version number!**

  <!-- NOTE: import from "__" folder -->
  <script src="https://cdn.jsdelivr.net/gh/paulshorey/pauls-pure-functions@latest/__/index.js"></script>

  <!-- or get a specific set of functions, without loading the whole library: -->
  <script src="https://cdn.jsdelivr.net/gh/paulshorey/pauls-pure-functions@latest/__/sort_strings.js"></script>

The newly created window.__ object of functions will contain one object called _map. It's a reference to which file each function originates from.

Library of functions:

For example: sort_by_rating_and_position() algorithmic sort function:

You have a list of cities, ordered from your most to least favorite, and a cost of living index for each:

let cities = ['Zurich', 'Paris', 'Kansas City', 'New York', 'Copenhagen', 'Duluth']
let ratings = { 'Zurich':1, 'Paris':1, 'Kansas City':10, 'New York':5, 'Copenhagen':5, 'Duluth':7 }

console.log(sort_by_rating_and_position(cities, ratings, 0.1))

The output will be something like this:

['Kansas City', 'New York', 'Duluth', 'Copenhagen', 'Zurich', 'Paris']

'Kansas City' jumped 2 spots, because it's rating was very high. Duluth jumped one spot ahead because it's rating was also pretty high. The first 2 cities dropped far down because their ratings were very low compared to the others.

The third parameter specifies how much preference is given to the original order. High number will make it hardly budge at all. Low number will make higher rated items float to the top more easily. See documentation.

This is much more useful with a much larger dataset of course!

Universal functions:

str_hash() to make a unique number out of a long or short string of text. Not for security or cryptography, but helps a lot to make a unique ID.

syllable_count() returns number of syllables for a given word

arr_remove_item() removes a value from an array, by value

ends_in_vowel() returns true if the input word ends in a vowel

sort_objects_by_property() just like sort_by_rating_and_position(), but instead of taking a list of items and object of ratings as separate parameters, it takes a list of objects, and sorts the list based on a property of each object.

sleep() returns a Promise after a number of milliseconds. Use with async/await to mimic other programming languages like Bash

for_each_promise_all() returns a Promise - like a [].forEach, executes the function on each value in array - but with Promises

call_later() a taste of curry - define the function and arguments seprately, to be called later.

parse_error_message() Error messages come in all shapes and sizes. From simple string, to standard new Error('...') to Axios/fetch promises containing an error... Returns a string which you can display to your user, without fear.

For browser only:

load_script() loads a script file into your document

object_from_querystring() and other URL utils

is_retina() true if on MacBook for example, or any other "retina" or higher resolution screen

For node.js only:

parse_cli_args() gets arguments passed to NodeJS process when it was started

And many more

As I make write (or find on Stackoverflow) a new function for myself/clients/employers, I copy it to this library. Please contribute your own. Let's combine efforts! See CodePen example. Play around with all the functions.

Documentation pages coming soon

For now, please read the JSDoc (comments) above each function in the ./src folder. Please bear with me. I'm currently completing all the documentation. After that's done, will compile it into several markdown files in the ./docs folder and link to them. arrays_diff

About

I use these functions in https://besta.domains, https://wordio.co, https://paulshorey.com, and others. I treat these as my code snippets. As I program something, and come accross a useful snippet, I make a function of it, and store it in this library. If you'd like to contribute, please don't be shy. Lets build something together! ~ Paul Shorey .com

matrix_flatten_to_strings

0.3.17

3 years ago

0.3.18

3 years ago

0.3.15

3 years ago

0.3.14

3 years ago

0.3.13

3 years ago

0.3.12

3 years ago

0.3.11

3 years ago

0.3.9

3 years ago

0.3.10

3 years ago

0.3.8

3 years ago

0.3.6

3 years ago

0.3.7

3 years ago

1.0.0

3 years ago

0.2.15

3 years ago

0.2.14

3 years ago

0.2.13

3 years ago

0.2.12

3 years ago

0.2.11

3 years ago

0.3.0

3 years ago

0.3.5

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.2.10

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago