Skip to content

Commit

Permalink
Add setWidth and setHeight methods to Format class
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitrinicolas committed Aug 27, 2018
1 parent 59a7047 commit 74de278
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Change Log
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1.0 - 2018-08-27
### Added
- The methods setWidth and setHeight to Format class.
### Changed
- Format.pixelCount is now a getter.

## 1.0.0 - 2018-08-26
- Initial release.
15 changes: 7 additions & 8 deletions README.md
Expand Up @@ -25,7 +25,6 @@ const compatibleAspects = resolutions.getMatchingAspect(
alternativeNames: [],
width: 2048,
height: 1080,
pixelCount: 2211840,
aspects: {
storage: Aspect { string: '256:135', float: 1.8962962962962964 },
display: Aspect { string: '1.90:1', float: 1.9 },
Expand Down Expand Up @@ -63,11 +62,17 @@ ratio
Each format `code` is either an unique string or `null`.

The `Format` class has a getter `resolution` returning a string of the width
and height merged around a "×" symbol.
and height merged around a "×" symbol, and a getter `pixelCount` returning the
multiplication of the number of pixels of width and height.

`aspect` is always a shorthand for `aspects.storage`, either as a getter of
the `Format` class or in a query object.

You can use the methods `setWidth(width)` and `setHeight(height)` to edit a
format width or height. The respectives height or width will be adapted to this
change to match the aspect-ratio. By changing width or height, the format will
lose its `code`, `name`, `fullName`, and `alternativeNames`.

### `getList()`

```js
Expand All @@ -84,7 +89,6 @@ resolutions.getList();
alternativeNames: [ 'Microvision' ],
width: 16,
height: 16,
pixelCount: 256,
aspects: {
storage: Aspect { string: '1:1', float: 1 },
display: Aspect { string: '1:1', float: 1 },
Expand Down Expand Up @@ -133,7 +137,6 @@ Format {
alternativeNames: [],
width: 4096,
height: 2160,
pixelCount: 8847360,
aspects: {
storage: Aspect { string: '256:135', float: 1.8962962962962964 },
display: Aspect { string: '1.90:1', float: 1.9 },
Expand Down Expand Up @@ -164,7 +167,6 @@ resolutions.getMatchingAspect(
alternativeNames: [],
width: 2048,
height: 1080,
pixelCount: 2211840,
aspects: {
storage: Aspect { string: '256:135', float: 1.8962962962962964 },
display: Aspect { string: '1.90:1', float: 1.9 },
Expand Down Expand Up @@ -192,7 +194,6 @@ resolutions.search('4k');
alternativeNames: [],
width: 4096,
height: 2160,
pixelCount: 8847360,
aspects: {
storage: Aspect { string: '256:135', float: 1.8962962962962964 },
display: Aspect { string: '1.90:1', float: 1.9 },
Expand All @@ -209,7 +210,6 @@ resolutions.search('4k');
alternativeNames: [ '2160p', '4000-lines UHDTV (4K UHD)' ],
width: 3840,
height: 2160,
pixelCount: 8294400,
aspects: {
storage: Aspect { string: '16:9', float: 1.7777777777777777 },
display: Aspect { string: '16:9', float: 1.7777777777777777 },
Expand All @@ -226,7 +226,6 @@ resolutions.search('4k');
alternativeNames: [],
width: 3840,
height: 1600,
pixelCount: 6144000,
aspects: {
storage: Aspect { string: '2.35:1', float: 2.35 },
display: Aspect { string: '2.35:1', float: 2.35 },
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "video-resolutions",
"version": "1.0.0",
"description": "",
"version": "1.1.0",
"description": "Search into a local database of 185 video resolutions and aspect-ratio",
"main": "src/index.js",
"author": "Dimitri NICOLAS <dimitri@fivecorp.fr>",
"license": "MIT",
Expand Down
36 changes: 32 additions & 4 deletions src/format.js
Expand Up @@ -25,10 +25,6 @@ class Format {
this.width = typeof data.width === 'number' ? data.width : null;
this.height = typeof data.height === 'number' ? data.height : null;

this.pixelCount = this.width !== null && this.height !== null
? this.width * this.height
: null;

this.aspects = {};

if (typeof data.aspects !== 'object') {
Expand Down Expand Up @@ -59,9 +55,41 @@ class Format {
: null;
}

get pixelCount() {
return this.width !== null && this.height !== null
? this.width * this.height
: null;
}

get aspect() {
return this.aspects[DEFAULT_ASPECT];
}

setHeight(height) {
if (this.width !== null && this.height !== null) {
this.width = (this.width / this.height) * height;
}
if (this.height !== height) {
this.code = null;
this.name = null;
this.fullName = null;
this.alternativeNames = [];
}
this.height = height;
}

setWidth(width) {
if (this.width !== null && this.height !== null) {
this.height = (this.height / this.width) * width;
}
if (this.width !== width) {
this.code = null;
this.name = null;
this.fullName = null;
this.alternativeNames = [];
}
this.width = width;
}
}

module.exports = Format;
16 changes: 16 additions & 0 deletions src/index.js
Expand Up @@ -15,3 +15,19 @@ module.exports = {
search,
searchOne
};

const format = getOne({
width: 1920,
height: 1080
});

format.setWidth(format.height * 2);

console.log(format);

console.log(
getOne({
width: 1920,
height: 1080
})
);

0 comments on commit 74de278

Please sign in to comment.