Giter Site home page Giter Site logo

codelytv / typescript-primitives-type Goto Github PK

View Code? Open in Web Editor NEW
107.0 5.0 9.0 505 KB

🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

Home Page: https://www.npmjs.com/package/@codelytv/primitives-type

License: GNU General Public License v3.0

JavaScript 8.79% TypeScript 91.21%
typescript typescript-library typescript-types typescript-utilities typescript-utility

typescript-primitives-type's Introduction

🧩 TypeScript Primitives type

Codely Open Source CodelyTV Courses

TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects).

Take a look, play and have fun with this. Stars are welcome 😊

πŸ‘€ Usage example

You can have a domain entity such as the following Course:

import { CourseId } from "./CourseId";
import { CourseTitle } from "./CourseTitle";

export class Course {
  constructor(public courseId: CourseId, public courseTitle: CourseTitle) {}

  updateTitle(newCourseTitle: CourseTitle): void {
    this.courseTitle = newCourseTitle;
  }

  someOtherMethodWithDomainLogic(): void {
    // some algorithm
  }
}

When we want to have a toPrimitives method in order to pass an instance of the Course class between architectural layers, so the thing we want pass around should be to serialized/marshalled in a way that only containings primitive values such as:

{
  "courseId": "25658f31-2da1-4942-8b58-88aeacc79860",
  "courseTitle": "🚜 TypeScript Avanzado: MÑs allÑ de any"
}

That is:

  • We want to avoid including methods such as the thisFunctionShouldNotBeIncludedInTheToPrimitives one, so the transformation should only include properties.
  • We want to flatten or unwrap encapsulated properties such as the courseId and courseTitle ones. They are modelled as Value Objects (CourseId and CourseTitle classes), so the transformation or flatten should only include properties in a recursive manner.

That is exactly what our Primitives<T> utility type guarantees. Let's add it! πŸ’ͺ

import { Primitives } from "@codelytv/primitives-type";

import { CourseId } from "./CourseId";
import { CourseTitle } from "./CourseTitle";

export class Course {
  constructor(public courseId: CourseId, public courseTitle: CourseTitle) {}

  updateTitle(newCourseTitle: CourseTitle): void {
    this.courseTitle = newCourseTitle;
  }

  someOtherMethodWithDomainLogic(): void {
    // some algorithm
  }

  toPrimitives(): Primitives<Course> {
    return {
      courseId: this.courseId.value,
      courseTitle: this.courseTitle.value,
    };
  }
}

Done! ✌️

The Primitives<Course> return type is ensuring all our restrictions in a very straightforward way! 🌈

πŸ‘ How to install

  • Npm: npm install --save-dev @codelytv/primitives-type
  • Yarn: yarn add -D @codelytv/primitives-type

πŸ‘ŒΒ Codely Code Quality Standards

Publishing this package we are committing ourselves to the following code quality standards:

  • 🀝 Respect Semantic Versioning: No breaking changes in patch or minor versions
  • 🀏 No surprises in transitive dependencies: Use the bare minimum dependencies needed to meet the purpose
  • 🎯 One specific purpose to meet without having to carry a bunch of unnecessary other utilities
  • βœ…Β Tests as documentation and usage examples
  • πŸ“– Well documented ReadMe showing how to install and use
  • βš–οΈ License favoring Open Source and collaboration

πŸ”€ Related utilities and resources

☝️ Learning resources

πŸ”· TypeScript skeletons

🌈 TypeScript Domain-Driven Design repositories

🎯 Other languages Domain-Driven Design repositories

typescript-primitives-type's People

Contributors

arnold095 avatar aronmoles avatar ismanapa avatar javiercane avatar rgomezcasas avatar rjurado01 avatar xetxeberria avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

typescript-primitives-type's Issues

Nullable attributes

Hi,

I have tried to use the library but I have problems with null values.

In a class with some attribute of type VideoName | null, the return type is VideoName | null when it should be string | null

Is it possible to fix this?

Regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.