Skip to content

iterUniqueBy treats iterator return value as part of sequence #468

@lightmare

Description

@lightmare

Description

iterUniqueBy calls the "uniqueness" predicate function on the iterator's return value. This is wrong, the return value is not part of the sequence, and cannot be skipped. A yield* ... expression doesn't distinguish between missing value and undefined.

source:
https://github.com/stdlib-js/iter-unique-by/blob/3c5a1bd4b7343469b5f03a9778c4e5b4db435454/lib/main.js#L139-L147

Related Issues

Haven't found any related to iter-unique

Questions

No.

Demo

No response

Reproduction

import iterUniqueBy from "@stdlib/iter-unique-by"

function notSameLength(a, b) {
  return a.length !== b.length
}

const filtered = iterUniqueBy(["baba", "yaga"].values(), notSameLength)

console.log(Array.from(filtered))

Expected Results

// output
[ 'baba' ]

Actual Results

file:///tmp/bug/repro.js:4
  return a.length !== b.length
                        ^

TypeError: Cannot read properties of undefined (reading 'length')
    at notSameLength (file:///tmp/bug/repro.js:4:25)
    at isUnique (/tmp/bug/node_modules/.pnpm/@[email protected]/node_modules/@stdlib/iter-unique-by/lib/main.js:59:19)
    at Object.next (/tmp/bug/node_modules/.pnpm/@[email protected]/node_modules/@stdlib/iter-unique-by/lib/main.js:142:38)
    at Function.from (<anonymous>)
    at file:///tmp/bug/repro.js:9:19

Version

@stdlib/iter-unique-by 0.0.6

Environments

Node.js

Browser Version

No response

Node.js / npm Version

Node.js v17.2.0 / pnpm 6.23.6

Platform

No response

Checklist

  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions