Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added array-upsert package #499

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

TClark1011
Copy link
Contributor

Closes #481

@angus-c
Copy link
Owner

angus-c commented Jan 28, 2023

Thanks this is really nice!
One suggestion:
If the requested index is negative the prepend the value to the start of the array, Has a nice symmetry to it, and feels like no one would intentionally supply a negative index if they wanted the value to go on the end.
What do you think?

@TClark1011
Copy link
Contributor Author

I prefer the more straightforward simplistic logic, eg;

Does the index exist in the array?

  • Yes: Insert into that index
  • No: Push to the end of the array

I think the extra logic behind "no" with "is the index negative" goes outside the scope of this function, and its very easy for someone to check that on their own. I agree that its rare someone would pass a negative index to the function in order to prepend, but ultimately, I think keeping the function straightforward is more worth it. Someone reading code who saw this function could reasonably assume how it functions, and they probably would not think it would prepend if the array is negative.

I always prefer to write functions that are narrow in scope, but that's just my opinion

@angus-c
Copy link
Owner

angus-c commented Feb 1, 2023

I always prefer to write functions that are narrow in scope

Same. Ok sounds good.

Copy link
Owner

@angus-c angus-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay in reviewing this. A few smallish things — biggest is I don't think we should limit type of upserted element to be the same as the type in the array.

@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 angus croll
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2023, add your own name too

import upsert from 'just-upsert';

upsert([1,2,3,4],-1,2) // [1,2,-1,4]
upsert(['a','b','c'],'d',6) // ['a','b','c','d']
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add example with negative third arg too, just to resolve ambiguity

upsert([1, 2, 3], 5, 'a');

// @ts-expect-error
upsert(['a', 'b', 'c'], 5, 2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be valid (see comment in d.ts file)

@@ -0,0 +1,3 @@
declare function upsert<T>(arr: T[], newValue: T, targetIndex: number): T[];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should limit new value to type of existing elements.
So upsert([1,2,3], 'a', 2) should be valid

@@ -0,0 +1,223 @@
const upsert = require('../../packages/array-upsert');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice thorough tests!

@TClark1011
Copy link
Contributor Author

@angus-c sorry for the huge delay, got distracted by stuff and forgot about this entirely, You can actually add an item of a different type to an array by manually specifying the generics, which I accounted for in the test.ts file:

upsert<number | string>(['a', 'b', 'c'], 5, 2);

I've also updated the package to match the newer structure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Package Idea: array upsert
2 participants