selfrefactor / rambda

Faster and smaller alternative to Ramda
https://selfrefactor.github.io/rambda
MIT License
1.65k stars 89 forks source link

corrected implementation of unless function #726

Closed orangebokov closed 8 months ago

orangebokov commented 8 months ago

This is a type definition from node_modules/rambda/index.d.ts

export function unless<T, U>(predicate: (x: T) => boolean, whenFalseFn: (x: T) => U, x: T): T | U;
export function unless<T, U>(predicate: (x: T) => boolean, whenFalseFn: (x: T) => U): (x: T) => T | U;
export function unless<T>(predicate: (x: T) => boolean, whenFalseFn: (x: T) => T, x: T): T;
export function unless<T>(predicate: (x: T) => boolean, whenFalseFn: (x: T) => T): (x: T) => T;

This is implementation from node_modules/rambda/src/unless.js

export function unless(predicate, whenFalse){
  if (arguments.length === 1){
    return _whenFalse => unless(predicate, _whenFalse)
  }

  return input => predicate(input) ? input : whenFalse(input)
}

The problem that this incorrectly works with 3 parameters. Example:

export const getCurrentURL = () => unless(startsWith('/'), concat('/'), window.location.pathname)

This should return the string. But it returns the function: input => predicate(input) ? input : whenFalse(input) Which is not surprising based on the implementation

The when function works great An example of correct implementation based on the when function:

import { curry } from './curry.js'

function unlessFn(predicate, whenFalseFn, input){
  if (predicate(input)) return input

  return whenFalseFn(input)
}

export const unless = curry(unlessFn)
selfrefactor commented 8 months ago

thanks for this change. I will notify once it is released.

orangebokov commented 8 months ago

thanks for the library

agriffis commented 7 months ago

I think this was broken by #727 somehow

selfrefactor commented 7 months ago

should be fixed with 9.2.0

agriffis commented 7 months ago

@selfrefactor rambdax release too?

selfrefactor commented 6 months ago

Sorry for delay, 11.1.0 for Rambdax was just released