Skip to content

Commit

Permalink
Add getAspectRatio And getGCD (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueleite42 authored Dec 28, 2021
1 parent 829c9b1 commit 66728bb
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 3 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

## [1.10.0] - 2021-12-28

### Added

- `getAspectRatio`
- `getGCD`

### Changed

### Fixed

### Removed

## [1.9.1] - 2021-12-04

### Added
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@

Package of utils, make in a way to let you import only the functions that you really need, so it doesn't make your project heavier than it needs to be.

**Every util is optimized to only contain the necessary code**, so even if this package have 1M utils, if you are using babel, webpack or @vercel/ncc, the compiled code will only contain the utils that you are using.

We also have **zero external dependencies**, so everything that can cause trouble to your project is inside this repository! It makes easier to check and validate if the code match your expectations and is good enough for your project.

## :warning: WARNING :warning:

The package may not be updated so often, but it not means that it has been abandoned. We do the utils in a way to (almost) never have to touch it again, and we only update the package to add new features, what may don't need to happen so often.

You can safely use this package, even if it don't receive updates in a year, and if you find any bug, you can report it by creating a issue in the GitHub repository, or create a PR to fix it! We will always give it special attention.

## Install

With Yarn:
Expand Down Expand Up @@ -61,7 +71,9 @@ npm i @techmmunity/utils
| function | description |
| ---------------------- | ---------------------------------------------------------------------- |
| `getArrayUniqueValues` | Remove duplicated values of an array (only work with primitive values) |
| `getAspectRatio` | Gets the aspect ratio (like 16:9, 4:3) |
| `getEnumValues` | Return the enum values |
| `getGCD` | Gets the Greatest Common Divisor (GCD) |
| `getHexColorLuma` | Return the color luma of a hex color |
| `getRootPath` | Get root path to something from the root folder of the process |
| `getTypeof` | Fix native "typeof" |
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module.exports = {
resetMocks: true,
coverageThreshold: {
global: {
statements: 99.15,
branches: 97.5,
statements: 99.2,
branches: 97.6,
functions: 100,
lines: 99.7,
},
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@techmmunity/utils",
"version": "1.9.1",
"version": "1.10.0",
"main": "index.js",
"types": "index.d.ts",
"license": "Apache-2.0",
Expand Down Expand Up @@ -29,6 +29,8 @@
"between",
"cpf",
"cnpj",
"aspect-ratio",
"gcd",
"empty-array",
"empty-object"
],
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export * from "./lib/unnest";
*/

export * from "./lib/get-array-unique-values";
export * from "./lib/get-aspect-ratio";
export * from "./lib/get-enum-values";
export * from "./lib/get-gcd";
export * from "./lib/get-hex-color-luma";
export { getRootPath } from "./lib/get-root-path";
export * from "./lib/get-typeof";
Expand Down
12 changes: 12 additions & 0 deletions src/lib/chunk/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
/* eslint-disable no-mixed-operators */
import { getTypeof } from "../get-typeof";

/**
* Divides an array in chunks with N items
*
* Ex:
* ```ts
* chunk([1, 2, 3], 1)
* // [[1], [2], [3]]
*
* chunk([1, 2, 3], 2)
* // [[1, 2], [3]]
* ```
*/
export const chunk = <T extends Array<any>>(
arr: T,
length: number,
Expand Down
10 changes: 10 additions & 0 deletions src/lib/get-aspect-ratio/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getGCD } from "../get-gcd";

/**
* Gets the aspect ratio (like 16:9, 4:3)
*/
export const getAspectRatio = (width: number, height: number) => {
const divisor = getGCD(width, height);

return `${width / divisor}:${height / divisor}`;
};
6 changes: 6 additions & 0 deletions src/lib/get-gcd/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Gets the Greatest Common Divisor (GCD)
* of 2 numbers
*/
export const getGCD = (a: number, b: number): number =>
b ? getGCD(b, a % b) : a;
131 changes: 131 additions & 0 deletions src/tests/get-aspect-ratio.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* eslint-disable sonarjs/no-duplicate-string */

import { getAspectRatio } from "../lib/get-aspect-ratio";

describe("getAspectRatio Util", () => {
describe("With width bigger than height", () => {
it("should return the aspect ratio (5:4)", () => {
let result: any;

try {
result = getAspectRatio(1280, 1024);
} catch (err: any) {
result = err;
}

expect(result).toBe("5:4");
});

it("should return the aspect ratio (6:5)", () => {
let result: any;

try {
result = getAspectRatio(1152, 960);
} catch (err: any) {
result = err;
}

expect(result).toBe("6:5");
});

it("should return the aspect ratio (4:3)", () => {
let result: any;

try {
result = getAspectRatio(1280, 960);
} catch (err: any) {
result = err;
}

expect(result).toBe("4:3");
});

it("should return the aspect ratio (16:9)", () => {
let result: any;

try {
result = getAspectRatio(1920, 1080);
} catch (err: any) {
result = err;
}

expect(result).toBe("16:9");
});
});

describe("With height bigger than width", () => {
it("should return the aspect ratio (4:5)", () => {
let result: any;

try {
result = getAspectRatio(1024, 1280);
} catch (err: any) {
result = err;
}

expect(result).toBe("4:5");
});

it("should return the aspect ratio (5:6)", () => {
let result: any;

try {
result = getAspectRatio(960, 1152);
} catch (err: any) {
result = err;
}

expect(result).toBe("5:6");
});

it("should return the aspect ratio (3:4)", () => {
let result: any;

try {
result = getAspectRatio(960, 1280);
} catch (err: any) {
result = err;
}

expect(result).toBe("3:4");
});

it("should return the aspect ratio (9:16)", () => {
let result: any;

try {
result = getAspectRatio(1080, 1920);
} catch (err: any) {
result = err;
}

expect(result).toBe("9:16");
});
});

describe("With width equal to height", () => {
it("should return the aspect ratio (1:1 - 1024)", () => {
let result: any;

try {
result = getAspectRatio(1024, 1024);
} catch (err: any) {
result = err;
}

expect(result).toBe("1:1");
});

it("should return the aspect ratio (1:1 - 960)", () => {
let result: any;

try {
result = getAspectRatio(960, 960);
} catch (err: any) {
result = err;
}

expect(result).toBe("1:1");
});
});
});
129 changes: 129 additions & 0 deletions src/tests/get-gcd.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { getGCD } from "../lib/get-gcd";

describe("getGCD Util", () => {
describe("With a bigger than b", () => {
it("should return the GCD (256)", () => {
let result: any;

try {
result = getGCD(1280, 1024);
} catch (err: any) {
result = err;
}

expect(result).toBe(256);
});

it("should return the GCD (192)", () => {
let result: any;

try {
result = getGCD(1152, 960);
} catch (err: any) {
result = err;
}

expect(result).toBe(192);
});

it("should return the GCD (320)", () => {
let result: any;

try {
result = getGCD(1280, 960);
} catch (err: any) {
result = err;
}

expect(result).toBe(320);
});

it("should return the GCD (120)", () => {
let result: any;

try {
result = getGCD(1920, 1080);
} catch (err: any) {
result = err;
}

expect(result).toBe(120);
});
});

describe("With b bigger than a", () => {
it("should return the GCD (256)", () => {
let result: any;

try {
result = getGCD(1024, 1280);
} catch (err: any) {
result = err;
}

expect(result).toBe(256);
});

it("should return the GCD (192)", () => {
let result: any;

try {
result = getGCD(960, 1152);
} catch (err: any) {
result = err;
}

expect(result).toBe(192);
});

it("should return the GCD (320)", () => {
let result: any;

try {
result = getGCD(960, 1280);
} catch (err: any) {
result = err;
}

expect(result).toBe(320);
});

it("should return the GCD (120)", () => {
let result: any;

try {
result = getGCD(1080, 1920);
} catch (err: any) {
result = err;
}

expect(result).toBe(120);
});
});

describe("With a equal to a", () => {
it("should return the GCD (1024)", () => {
let result: any;

try {
result = getGCD(1024, 1024);
} catch (err: any) {
result = err;
}

expect(result).toBe(1024);
});

it("should return the GCD (960)", () => {
let result: any;

try {
result = getGCD(960, 960);
} catch (err: any) {
result = err;
}

expect(result).toBe(960);
});
});
});

0 comments on commit 66728bb

Please sign in to comment.