Skip to content

Commit

Permalink
Merge pull request #81 from Bagaar/TS
Browse files Browse the repository at this point in the history
Migrate to TypeScript
  • Loading branch information
bertdeblock authored Feb 12, 2024
2 parents 3295170 + 2873a62 commit 95d62a6
Show file tree
Hide file tree
Showing 27 changed files with 2,816 additions and 2,323 deletions.
10 changes: 1 addition & 9 deletions .ember-cli
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
{
/**
Ember CLI sends analytics information by default. The data is completely
anonymous, but there are times when you might want to disable this behavior.

Setting `disableAnalytics` to true will prevent any data from being sent.
*/
"disableAnalytics": false,

/**
Setting `isTypeScriptProject` to true will force the blueprint generators to generate TypeScript
rather than JavaScript by default, when a TypeScript version of a given blueprint is available.
*/
"isTypeScriptProject": false
"isTypeScriptProject": true
}
23 changes: 11 additions & 12 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

module.exports = {
root: true,
parser: '@babel/eslint-parser',
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
requireConfigFile: false,
babelOptions: {
plugins: [
['@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true }],
],
},
},
plugins: ['ember'],
plugins: ['ember', '@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:ember/recommended',
Expand All @@ -24,6 +17,15 @@ module.exports = {
},
rules: {},
overrides: [
// ts files
{
files: ['**/*.ts'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {},
},
// node files
{
files: [
Expand All @@ -39,9 +41,6 @@ module.exports = {
'./config/**/*.js',
'./tests/dummy/config/**/*.js',
],
parserOptions: {
sourceType: 'script',
},
env: {
browser: false,
node: true,
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install Node
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18
cache: yarn
- name: Install Dependencies
run: yarn install --frozen-lockfile
Expand All @@ -40,7 +40,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18
cache: yarn
- name: Install Dependencies
run: yarn install --no-lockfile
Expand All @@ -57,7 +57,6 @@ jobs:
fail-fast: false
matrix:
try-scenario:
- ember-lts-4.4
- ember-lts-4.8
- ember-lts-4.12
- ember-release
Expand All @@ -71,7 +70,7 @@ jobs:
- name: Install Node
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18
cache: yarn
- name: Install Dependencies
run: yarn install --frozen-lockfile
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# compiled output
/dist/
/declarations/

# dependencies
/node_modules/
Expand Down
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
/ember-cli-build.js
/testem.js
/tests/
/types/
/tsconfig.declarations.json
/tsconfig.json
/yarn-error.log
/yarn.lock
.gitkeep
Expand Down
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

## Running tests

* `ember test` – Runs the test suite on the current Ember version
* `ember test --server` – Runs the test suite in "watch mode"
* `ember try:each` – Runs the test suite against multiple Ember versions
* `yarn test` – Runs the test suite on the current Ember version
* `yarn test:ember --server` – Runs the test suite in "watch mode"
* `yarn test:ember-compatibility` – Runs the test suite against multiple Ember versions

## Running the dummy application

* `ember serve`
* `yarn start`
* Visit the dummy application at [http://localhost:4200](http://localhost:4200).

For more information on using ember-cli, visit [https://cli.emberjs.com/release/](https://cli.emberjs.com/release/).
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ Easy customisable pagination component for Ember applications.

## Compatibility

- Ember.js v4.4 or above
- Ember CLI v4.4 or above
- Node.js v16 or above
- Ember.js v4.8 or above
- Ember CLI v4.8 or above
- Node.js v18 or above

## Installation

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,60 @@
import { assert } from '@ember/debug';
import Component from '@glimmer/component';

interface PaginationDataSignature {
Args: {
currentPage: number;
itemsPerPage: number;
pageMargins?: number;
pageRange?: number;
totalItems: number;
};
Blocks: {
default: [
{
activeItems: number;
allPages: number[];
currentPage: number;
endMarginPages: number[] | null;
firstActiveItem: number;
isFirstPage: boolean;
isLastPage: boolean;
itemsPerPage: number;
lastActiveItem: number;
lastPage: number;
nextPage: number | null;
pageMargins: number;
pageRange: number | null;
pageRangePages: number[] | null;
previousPage: number | null;
shouldShowLowerBreak: boolean;
shouldShowUpperBreak: boolean;
startMarginPages: number[] | null;
totalItems: number;
totalPages: number;
},
];
};
}

const DISABLED = null;
const FIRST_PAGE = 1;

export default class PaginationDataComponent extends Component {
export default class PaginationData extends Component<PaginationDataSignature> {
/**
* Argument getters
*/

get currentPage() {
assert(
`@currentPage is required and must be a number. You provided \`${this.args.currentPage}\`.`,
isNumber(this.args.currentPage)
typeof this.args.currentPage === 'number',
);

assert(
`@currentPage must be a value between \`${FIRST_PAGE}\` and \`${this.lastPage}\`. You provided \`${this.args.currentPage}\`.`,
this.args.currentPage >= FIRST_PAGE &&
this.args.currentPage <= this.lastPage
this.args.currentPage <= this.lastPage,
);

return this.args.currentPage;
Expand All @@ -27,33 +63,33 @@ export default class PaginationDataComponent extends Component {
get itemsPerPage() {
assert(
`@itemsPerPage is required and must be a number. You provided \`${this.args.itemsPerPage}\`.`,
isNumber(this.args.itemsPerPage)
typeof this.args.itemsPerPage === 'number',
);

return this.args.itemsPerPage;
}

get pageMargins() {
if (isNumber(this.args.pageMargins) === false) {
if (typeof this.args.pageMargins !== 'number') {
return 1;
}

assert(
`@pageMargins must be a number higher than \`0\`. You provided \`${this.args.pageMargins}\`.`,
this.args.pageMargins > 0
this.args.pageMargins > 0,
);

return this.args.pageMargins;
}

get pageRange() {
if (isNumber(this.args.pageRange) === false) {
if (typeof this.args.pageRange !== 'number') {
return null;
}

assert(
`@pageRange must be an uneven number to make sure that the active page is always center aligned. You provided \`${this.args.pageRange}\`.`,
this.args.pageRange % 2 !== 0
this.args.pageRange % 2 !== 0,
);

return this.args.pageRange;
Expand All @@ -62,7 +98,7 @@ export default class PaginationDataComponent extends Component {
get totalItems() {
assert(
`@totalItems is required and must be a number. You provided \`${this.args.totalItems}\`.`,
isNumber(this.args.totalItems)
typeof this.args.totalItems === 'number',
);

return this.args.totalItems;
Expand Down Expand Up @@ -133,7 +169,7 @@ export default class PaginationDataComponent extends Component {
}

get pageMarginsThreshold() {
return this.pageRange + this.pageMargins * 2;
return (this.pageRange || 0) + this.pageMargins * 2;
}

get pageRangeLowerLimit() {
Expand Down Expand Up @@ -185,7 +221,8 @@ export default class PaginationDataComponent extends Component {
}

return (
this.pageRangePages.length &&
Array.isArray(this.pageRangePages) &&
this.pageRangePages.length > 0 &&
this.pageRangePages[0] !== this.pageRangeLowerLimit
);
}
Expand All @@ -200,7 +237,8 @@ export default class PaginationDataComponent extends Component {
}

return (
this.pageRangePages.length &&
Array.isArray(this.pageRangePages) &&
this.pageRangePages.length > 0 &&
this.pageRangePages[this.pageRangePages.length - 1] !==
this.pageRangeUpperLimit
);
Expand All @@ -225,14 +263,10 @@ export default class PaginationDataComponent extends Component {
}
}

function clamp(number, min, max) {
function clamp(number: number, min: number, max: number) {
return Math.min(Math.max(number, min), max);
}

function isNumber(value) {
return typeof value === 'number' && isNaN(value) === false;
}

function range(start, end) {
function range(start: number, end: number) {
return new Array(end - start + 1).fill(undefined).map((_, i) => i + start);
}
5 changes: 5 additions & 0 deletions addon/template-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type PaginationData from '@bagaar/ember-pagination/components/pagination-data';

export default interface Registry {
PaginationData: typeof PaginationData;
}
4 changes: 3 additions & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');

module.exports = function (defaults) {
const app = new EmberAddon(defaults, {
// Add options here
'ember-cli-babel': {
enableTypeScriptTransform: true,
},
});

/*
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

module.exports = {
name: require('./package').name,

options: {
'ember-cli-babel': {
enableTypeScriptTransform: true,
},
},
};
Loading

0 comments on commit 95d62a6

Please sign in to comment.