You Don't Need A Dependency For Clamp

Поділитися
Вставка
  • Опубліковано 19 гру 2024

КОМЕНТАРІ • 6

  • @xpdmk
    @xpdmk 2 роки тому

    If you want the order of boundary values to not matter, clamp can be implemented in the following way:
    const clamp = (num, a, b) => Math.min(Math.max(num, Math.min(a, b)), Math.max(a, b))

  • @oskrm
    @oskrm 2 роки тому

    Scott, have you tried svelte-trpc?

    • @syntaxfm
      @syntaxfm  2 роки тому

      I've never used tRPC in general. Looks interesting though.

  • @rtorcato
    @rtorcato 2 роки тому

    If you are using lodash in your project they already have a clamp function. console.log(_.clamp(2, 3, 5));

    • @syntaxfm
      @syntaxfm  2 роки тому +2

      I'd argue that lodash isn't a dependency you need in 2022, at the very least, you shouldn't be loading the entire lodash library.

    • @rtorcato
      @rtorcato 2 роки тому

      @@syntaxfm a lot of projects use lodash and it maybe. dependancy already and you can now cherrypick lodash functions. var clamp = require('lodash.clamp');