Skip to content

Commit

Permalink
decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
raj-rathod committed Dec 28, 2023
1 parent 2957f68 commit 6e2ae99
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.DS_Store
node_modules
dist
index.html
55 changes: 54 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
{"version":"0.1.0","license":"MIT","main":"dist/index.js","typings":"dist/index.d.ts","files":["dist","src"],"engines":{"node":">=10"},"scripts":{"start":"tsdx watch","build":"tsdx build","test":"tsdx test","lint":"tsdx lint","prepare":"tsdx build","size":"size-limit","analyze":"size-limit --why"},"husky":{"hooks":{"pre-commit":"tsdx lint"}},"prettier":{"printWidth":80,"semi":true,"singleQuote":true,"trailingComma":"es5"},"name":"typescript","author":"Rajesh Rathore","module":"dist/typescript.esm.js","size-limit":[{"path":"dist/typescript.cjs.production.min.js","limit":"10 KB"},{"path":"dist/typescript.esm.js","limit":"10 KB"}],"devDependencies":{"@size-limit/preset-small-lib":"^8.2.6","husky":"^8.0.3","size-limit":"^8.2.6","tsdx":"^0.14.1","tslib":"^2.6.0","typescript":"^3.9.10"}}
{
"version": "0.1.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
],
"engines": {
"node": ">=10"
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"test": "tsdx test",
"lint": "tsdx lint",
"prepare": "tsdx build",
"size": "size-limit",
"analyze": "size-limit --why"
},
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
}
},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
},
"name": "typescript",
"author": "Rajesh Rathore",
"module": "dist/typescript.esm.js",
"size-limit": [
{
"path": "dist/typescript.cjs.production.min.js",
"limit": "10 KB"
},
{
"path": "dist/typescript.esm.js",
"limit": "10 KB"
}
],
"devDependencies": {
"@size-limit/preset-small-lib": "^8.2.6",
"husky": "^8.0.3",
"size-limit": "^8.2.6",
"tsdx": "^0.14.1",
"tslib": "^2.6.0",
"typescript": "^3.9.10"
}
}
9 changes: 9 additions & 0 deletions src/Decorators/deprecated-decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function deprecated(target: any, key: string, descriptor: PropertyDescriptor) {

Check failure on line 1 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 12.x and ubuntu-latest

Replace `target:·any,·key:·string,·descriptor:·PropertyDescriptor` with `⏎··target:·any,⏎··key:·string,⏎··descriptor:·PropertyDescriptor⏎`

Check failure on line 1 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Replace `target:·any,·key:·string,·descriptor:·PropertyDescriptor` with `⏎··target:·any,⏎··key:·string,⏎··descriptor:·PropertyDescriptor⏎`
const originalDef = descriptor.value;

Check failure on line 2 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 12.x and ubuntu-latest

Delete `··`

Check failure on line 2 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Delete `··`
console.log(target)

Check failure on line 3 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 12.x and ubuntu-latest

Replace `···console.log(target)` with `console.log(target);`

Check failure on line 3 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Replace `···console.log(target)` with `console.log(target);`
descriptor.value = function (...args: any[]) {

Check failure on line 4 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 12.x and ubuntu-latest

Replace `··descriptor.value·=·function·` with `descriptor.value·=·function`

Check failure on line 4 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Replace `··descriptor.value·=·function·` with `descriptor.value·=·function`
console.log(`Warning: ${key}() is deprecated. Use other methods instead.`);

Check failure on line 5 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 12.x and ubuntu-latest

Delete `··`

Check failure on line 5 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Delete `··`
return originalDef.apply(this, args);

Check failure on line 6 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 12.x and ubuntu-latest

Delete `··`

Check failure on line 6 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Delete `··`
};

Check failure on line 7 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 12.x and ubuntu-latest

Delete `··`

Check failure on line 7 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Delete `··`
return descriptor;

Check failure on line 8 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 12.x and ubuntu-latest

Delete `··`

Check failure on line 8 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Delete `··`
}

Check failure on line 9 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 12.x and ubuntu-latest

Replace `··}` with `}⏎`

Check failure on line 9 in src/Decorators/deprecated-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Replace `··}` with `}⏎`
7 changes: 7 additions & 0 deletions src/Decorators/enumerable-decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function enumerable(isEnumerable: boolean) {
return (target: any, key: string, descriptor: PropertyDescriptor) => {

Check failure on line 2 in src/Decorators/enumerable-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 12.x and ubuntu-latest

Delete `··`

Check failure on line 2 in src/Decorators/enumerable-decorator.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Delete `··`
console.log(target,key);
descriptor.enumerable = isEnumerable;
console.log("The enumerable property of this member is set to: " + descriptor.enumerable);
};
}
4 changes: 4 additions & 0 deletions src/Decorators/frozen.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function frozen(target: Function) {
Object.freeze(target);
Object.freeze(target.prototype);
}
13 changes: 13 additions & 0 deletions src/Decorators/required-decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function required(target: any, key: string) {
let currentValue = target[key];

Object.defineProperty(target, key, {
set: (newValue: string) => {
if (!newValue) {
throw new Error(`${key} is required.`);
}
currentValue = newValue;
},
get: () => currentValue,
});
}
1 change: 1 addition & 0 deletions src/Modules/types_in.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const Rajesh='Rajesh'
52 changes: 47 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('boop');
import { deprecated } from "./Decorators/deprecated-decorator";
import { enumerable } from "./Decorators/enumerable-decorator";
import { frozen } from "./Decorators/frozen.decorator";
import { required } from "./Decorators/required-decorator";

@frozen
class User {
private static userType: string = "Generic";

@required
private _email: string;

@required
public username: string;

public addressLine1: string = "";
public addressLine2: string = "";
public country: string = "";

constructor(username: string, email: string) {
this.username = username;
this._email = email;
}
return a + b;
};

@enumerable(false)
get userType() {
return User.userType;
}

get email() {
return this._email;
}

set email(newEmail: string) {
this._email = newEmail;
}

@deprecated
address(): any {
return `${this.addressLine1}\n${this.addressLine2}\n${this.country}`;
}
}

const p = new User("uut", "example@example.com");
p.addressLine1 = "1, New Avenue";
p.addressLine2 = "Bahcelievler, Istanbul";
p.address();
console.log(JSON.stringify(p));
6 changes: 0 additions & 6 deletions test/blah.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
import { sum } from '../src';

describe('blah', () => {
it('works', () => {
expect(sum(1, 1)).toEqual(2);
});
});
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
// error out if import and file system have a casing mismatch. Recommended by TS
"forceConsistentCasingInFileNames": true,
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc`
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noEmit": true,
}
}

0 comments on commit 6e2ae99

Please sign in to comment.