bs-sysdate
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

README

Ports and Adapters approach to keep low level details such as the system date out of the code. This is particularly useful for testing as you can create expect assertions against immutable date information. Brought to you by binary stars GmbH, written in TypeScript.

Installation

  • npm i --save bs-sysdate

How to use

In your production code in domain or application layer

import Sysdate from 'bs-sysdate'

class MyServiceBlahBlah {
    constructor(private sysdate: Sysdate) {}

    importSomething(inputData: any): any {
        return {
            key: inputData['id'],
            productName: inputData['name'],
            importDateTime: this.sysdate.now(),
        }
    }
}

In your production code in Dependency Injection, Factory etc.

import Sysdate, {SysdateRealImpl} from 'bs-sysdate'

const createMyService: MyServiceBlahBlah = () => {
    const sysdateSupplier = new SysdateRealImpl()  
    return new MyServiceBlahBlah(sysdateSupplier)
}

const myService = createMyService()

// call myService.importSomething for mAGiC 🪄

In your tests

import Sysdate, {SysdateRealImpl} from 'bs-sysdate'

const date = new Date('2022-01-14 23:42:06 GMT+01:00')
const sysdate = new SysdateFixedImpl(date)
const myService = new MyServiceBlahBlah(sysdate)

test('importSomething...', () => {
    // given
    const given = {
        id: '123',
        name: 'ACME TNT',
    }

    // when
    const actual = myService.importSomething(given)

    // then
    expect(actual.key).toEqual('123')
    expect(actual.productName).toEqual('ACME TNT')

    expect(actual.importDateTime.getUTCFullYear()).toBe(2022)
    expect(actual.importDateTime.getUTCMonth()).toBe(0)
    expect(actual.importDateTime.getUTCDate()).toBe(14)
    expect(actual.importDateTime.getUTCHours()).toBe(22) // it's UTC
    expect(actual.importDateTime.getUTCMinutes()).toBe(42)
    expect(actual.importDateTime.getUTCSeconds()).toBe(6)
})

FAQs

  • Q: "I want the date to be created in a specific way instead of being created via the default constructor new Date() like SysdateRealImpl does."
  • A: Use SysdateFactoryImpl and pass a function, e.g. new SysdateFactoryImpl(() => mySpecificDateCreationFunction())

Contribution guidelines

  • 100% code coverage
  • run npm run review to check style, eslint, run tests and check coverage

About us

  • binary stars GmbH, Hausinger Str. 8, 40764 Langenfeld, Germany
  • #clean-code #clean-architecture #tdd #xp #agile
  • We offer a CTO2Go for your software projects & products + developers, QA engineers and agile project managers.
  • Wanna know how we achieve 100% coverage EVERY TIME? Contact us about our training & consulting services.
  • Our open source projects: https://www.binary-stars.eu/en/open-source-projects/

Version History

  • 1.0.6
    • update vulnerable dependencies
    • remove annoying and unneded node version restriction
  • (1.0.5)
    • git is stupid...
  • 1.0.4
    • add SysdateFactoryImpl as common super class and potential extension point
    • add validation to SysdateFixedImpl to ensure date is set
    • improve README
  • 1.0.3
    • first working release

Package Sidebar

Install

npm i bs-sysdate

Weekly Downloads

5

Version

1.0.6

License

MIT

Unpacked Size

12.4 kB

Total Files

15

Last publish

Collaborators

  • binary-stars