Skip to content

[feature] add min and max at static methods #25

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

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

Conversation

itsmuntadhar
Copy link

First, I would like to say thanks for the great work!

We've been looking into ULID to use instead of UUID/GUID in our projects and figured we could get rid of the dedicated "CreatedAt" column/prop that we usually have.

To make querying easier, we thought such methods would be great to have in the lib so we could do something like

var anHourAgo = DateTimeOffset.UtcNow.AddHours(-1);
var idGte = Ulid.MinAt(anHourAgo);
queryable = queryable.Where(x => x.Id >= idGte);

Was also looking into writing a couple more extensions, maybe in another PR.
Basically

DateTimeOffset.UtcNow.NewUlid();
DateTimeOffset.UtcNow.MinUlid();
DateTimeOffset.UtcNow.MaxUlid();

Thanks again

@RobThree
Copy link
Owner

Hi!

I'm not sure why I missed this PR and never replied. I'll look into this soon(-ish) and get back to you. Just wanted to let you know your PR is appreciated and I apologize for missing it.

@itsmuntadhar
Copy link
Author

Things happen :)

Will look into solving the conflicts shortly

@RobThree RobThree requested a review from Copilot May 16, 2025 16:47
@RobThree RobThree self-assigned this May 16, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@RobThree RobThree requested a review from Copilot May 16, 2025 17:03
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces new static methods for generating ULIDs based on a specific DateTimeOffset, allowing users to retrieve the minimum and maximum ULID values for a given time.

  • Added Ulid.MinAt and Ulid.MaxAt static methods in Ulid.cs that generate ULIDs with fixed byte arrays for minimum and maximum values.
  • Updated the MinValue and MaxValue fields to use the new methods.
  • Provided corresponding DateTimeOffset extension methods (NewUlid, MinUlid, MaxUlid) in DateTimeExtensions.cs.
  • Updated global.json and VS Code settings files for configuration.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
global.json Updated SDK version configuration.
NUlid/Ulid.cs Added static methods for ULID generation with min/max values and updated MinValue/MaxValue fields.
NUlid/DateTimeExtensions.cs Added extension methods for DateTimeOffset to generate ULIDs.
.vscode/settings.json Configured default solution for VS Code.

/// <param name="time">The <see cref="DateTimeOffset"/> to use for the time-part of the <see cref="Ulid"/>.</param>
/// <returns>Returns a new <see cref="Ulid"/>.</returns>
public static Ulid MinAt(DateTimeOffset time)
=> new(time, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider extracting the fixed byte array [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] into a static readonly field to avoid new allocations on each call.

Suggested change
=> new(time, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
=> new(time, MinBytes);

Copilot uses AI. Check for mistakes.

Comment on lines +121 to +122
public static Ulid MaxAt(DateTimeOffset time)
=> new(time, new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 });
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider extracting the fixed byte array [255, 255, 255, 255, 255, 255, 255, 255, 255, 255] into a static readonly field to avoid new allocations on each call.

Suggested change
public static Ulid MaxAt(DateTimeOffset time)
=> new(time, new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 });
private static readonly byte[] MaxBytes = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
public static Ulid MaxAt(DateTimeOffset time)
=> new(time, MaxBytes);

Copilot uses AI. Check for mistakes.

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

Successfully merging this pull request may close these issues.

2 participants