Skip to content

Commit

Permalink
chore: make requested changes and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Sep 4, 2022
1 parent ca9d338 commit 0900139
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/builders/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ export * from './interactions/contextMenuCommands/ContextMenuCommandBuilder.js';
export * from './util/componentUtil.js';
export * from './util/normalizeArray.js';
export * from './util/validation.js';
export * from '@discordjs/util';
12 changes: 12 additions & 0 deletions packages/util/__tests__/Equatable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, test, expect } from 'vitest';
import { isEquatable } from '../src/index.js';

describe('isEquatable', () => {
test('returns true if the object is equatable', () => {
expect(isEquatable({ equals: () => true })).toBeTruthy();
});

test('returns false if the object is not equatable', () => {
expect(isEquatable({})).toBeFalsy();
});
});
19 changes: 19 additions & 0 deletions packages/util/__tests__/JSONEncodable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, test, expect } from 'vitest';
import { isJSONEncodable, type JSONEncodable } from '../src/index.js';

class Encodable implements JSONEncodable<{}> {
public toJSON() {
return {};
}
}

describe('isJSONEncodable', () => {
test('returns true if the object is JSON encodable', () => {
expect(isJSONEncodable({ toJSON: () => ({}) })).toBeTruthy();
expect(isJSONEncodable(new Encodable())).toBeTruthy();
});

test('returns false if the object is not JSON encodable', () => {
expect(isJSONEncodable({})).toBeFalsy();
});
});
32 changes: 32 additions & 0 deletions packages/util/__tests__/lazy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2020 The Sapphire Community and its contributors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://github.com/sapphiredev/utilities/blob/main/LICENSE.md.
*/

import { describe, test, expect, vi } from 'vitest';
import { lazy } from '../src/index.js';

describe('lazy', () => {
test('GIVEN string callback THEN returns the same', () => {
const callback = vi.fn(() => 'Lorem Ipsum');

const lazyStoredValue = lazy(callback);

expect(lazyStoredValue()).toEqual('Lorem Ipsum');
});

test('GIVEN string callback with cached value THEN returns the same', () => {
const callback = vi.fn(() => 'Lorem Ipsum');

const lazyStoredValue = lazy(callback);

lazyStoredValue();
const cachedValue = lazyStoredValue();

expect(callback).toHaveBeenCalledOnce();
expect(cachedValue).toEqual('Lorem Ipsum');
});
});
12 changes: 12 additions & 0 deletions packages/util/__tests__/range.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, test, expect } from 'vitest';
import { range } from '../src/index.js';

describe('range', () => {
test('GIVEN valid range THEN valid array is returned', () => {
expect(range(0, 5)).toEqual([0, 1, 2, 3, 4, 5]);
});

test('GIVEN valid range with step THEN valid array is returned', () => {
expect(range(0, 10, 2)).toEqual([0, 2, 4, 6, 8, 10]);
});
});
7 changes: 6 additions & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"private": true,
"scripts": {
"build": "tsup",
"test": "vitest run",
"lint": "prettier --check . && TIMING=1 eslint src --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src --ext mjs,js,ts --fix",
"fmt": "yarn format"
Expand All @@ -24,7 +25,11 @@
"dist"
],
"contributors": [
"Crawl <icrawltogo@gmail.com>"
"Crawl <icrawltogo@gmail.com>",
"Amish Shah <amishshah.2k@gmail.com>",
"Vlad Frangu <kingdgrizzle@gmail.com>",
"SpaceEEC <spaceeec@yahoo.com>",
"Antonio Roman <kyradiscord@gmail.com>"
],
"license": "Apache-2.0",
"keywords": [
Expand Down
11 changes: 9 additions & 2 deletions packages/util/src/functions/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
*
* @param start - The start of the range
* @param end - The end of the range (inclusive)
* @param step - The amount to increment between each number
* @example
* Basic range
* ```ts
* range(3, 5); // [3, 4, 5]
* ```
* @example
* Range with a step
* ```ts
* range(3, 10, 2); // [3, 5, 7, 9]
* ```
*/
export function range(start: number, end: number): number[] {
return Array.from({ length: end - start + 1 }, (_, index) => index + start);
export function range(start: number, end: number, step = 1): number[] {
return Array.from({ length: (end - start) / step + 1 }, (_, index) => start + index * step);
}

0 comments on commit 0900139

Please sign in to comment.