Skip to content

Instantly share code, notes, and snippets.

@Craphtex
Craphtex / format.js
Last active October 29, 2021 01:01
JavaScript function for replaces {n} in a string with the nth following parameter.
// Replaces {n} in a string with the nth following parameter.
const format = (text, ...values) => text.replace(/{([0-9]*)}/g, (m, i) => values[i] || m)
// Example 1
console.log(format('Hello {0}! How\'s your {1}?', 'my dear', 'day been'))
// Output: Hello my dear! How's your day been?
// Example 2
const list = ['the city', 'train', 'speed', 'convenient']
@Craphtex
Craphtex / get-tuples.js
Created November 28, 2019 02:21
Converts an array to an array of n-tuples of its elements.
/**
* Converts an array to an array of n-tuples of its elements.
*
* To generate a gap between the n-tuples a stride can be set. The stride is
* offsetting the starting elements in the derived n-tuples.
*
* span = 3 and stride = 2
* [1,2,3,4,5,6,7] -> [[1,2,3], [3,4,5], [5,6,7]]
*
* span = 2 and stride = 3

Keybase proof

I hereby claim:

  • I am craphtex on github.
  • I am craphtex (https://keybase.io/craphtex) on keybase.
  • I have a public key ASAh1sEH2fJ9FGgnVcKBM4pJHdsUWyS6zCXKZVlN1OGAzAo

To claim this, I am signing this object: