Giter VIP home page Giter VIP logo

Comments (6)

hueniverse avatar hueniverse commented on June 9, 2024

"should be possible just like it is in chai" - why not just use chai? Not everything should be extended.

from code.

coder13 avatar coder13 commented on June 9, 2024

Why shouldn't everything be able to be extended? That puts power in the hands of the developer and they can continue to use simple, yet powerful libraries. Being able to plug many simple libraries together to make exactly what one needs is far superior than having to use a few things from large libraries.

I'm using code because of it's simplicity but the fact that it can't be extended (which is as simple as exposing a single function) is something I feel is missing from this simple library.

from code.

hueniverse avatar hueniverse commented on June 9, 2024

Because once you allow extensions you need to maintain that API and in many cases, that will limit your ability to make internal changes. It's a long-term PITA. I belong to the No First school of open source leadership.

from code.

cjihrig avatar cjihrig commented on June 9, 2024

I'm going to close this for now, in the same spirit as #39. If anyone can convince @hueniverse that this is a good idea, then we can revisit :-)

from code.

dpmott avatar dpmott commented on June 9, 2024

(This comment is intended to detail a full work-around example and the issues with it, and is not intended to be a call to action.)

I would also enjoy the ability to extend Hapi Code to support a custom matcher. Unfortunately, in a typescript project I'm not sure how the @types/code definitions could be made to allow for a seamless use scenario.

Based on the example that @coder13 offered in their OP, I coded up what this might look like using Hapi Lab 18.0.1 / Hapi Code 5.2.4:

Object.getPrototypeOf(expect(null)).uuid = function () {
  const isUuid = (input: string) => {
    const regex = new RegExp('^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$', 'i');
    return regex.test(input);
  };

  // tslint:disable-next-line: no-invalid-this
  return Object.getPrototypeOf(this).assert.call(this, isUuid(this._ref));
};

export interface ExtendedValues<T> extends AssertionChain<T> {
  uuid(): AssertionChain<T>;
}

// later, in a test: this generates a warning: Property 'uuid' does not exist on type 'AssertionChain<any>'.
  expect('my guid').to.be.a.uuid();

// fixing with a typecast
    (expect('my guid').to.be.a as ExtendedValues<string>).uuid();

// or, if you're okay with casting as 'any':
    (expect('my guid').to.be.a any).uuid();

// or you could just tell Typescript to shut up about it:
  // @ts-ignore: uuid() extension
  expect('my guid').to.be.a.uuid();

Note: Replaced the use of deprecated __proto__ with Object.getPrototypeOf.

The real ugliness is that the grammar is typed, and knows what methods can follow, which would exclude anything layered into the internals.Assertion prototype because all of the types are implemented separately in @types/code.

from code.

lock avatar lock commented on June 9, 2024

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

from code.

Related Issues (20)

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.