Basic TypeScript Mapping Engine

Just lately I had a need for a mapping engine in TypeScript. I came up with the follwing basic design. I unfortunately couldn’t use it as it works with classes only and our API client generation tool returns types only. It might be of some use for those who do need to map classes however. Have fun with it!

export class MapEngine {
    private readonly mappers: Map<string, any> = new Map<string, any>();

    public get size(): number {
        return this.mappers.size;
    }

    public map<TSource, TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination,
        source: TSource ): TDestination {
            const key = this.createKey(sourceType, destinationType);

            if(!this.mappers.has(key)) {
                throw new Error(`No map defined for: ${key}`);
            }

            const mapper = this.mappers.get(key) as (source: TSource) => TDestination;
            const result = mapper(source);

            return result;
    }

    public set<TSource,TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination,
        map: (source: TSource) => TDestination): MapEngine {
            const key = this.createKey(sourceType, destinationType);

            this.mappers.set(key, map);

            return this;
    }

    public get<TSource,TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination): (source: TSource) => TDestination {
            const key = this.createKey(sourceType, destinationType);
            const result = this.mappers.get(key) as (source: TSource) => TDestination;

            return result;
    }

    public has<TSource,TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination): boolean {
            const key = this.createKey(sourceType, destinationType);
            const result = this.mappers.has(key);

            return result;
    }

    public delete<TSource,TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination): boolean {
            const key = this.createKey(sourceType, destinationType);
            const result = this.mappers.delete(key);

            return result;
    }

    public clear(): void {
            this.mappers.clear();
    }

    private createKey<TSource, TDestination>(
        sourceType: new (...args: any[]) => TSource,
        destinationType: new (...args: any[]) => TDestination): string {
            const result = `${sourceType.name} -> ${destinationType.name}`;

            return result;
    }
}

It can be used like this:

let mapEngine = new MapEngine();
mapEngine.set(Number, String, (source)=> source.toString());
mapEngine.set(Date, String, (source)=> source.toLocaleString());
mapEngine.set(Date, Number, (source)=> source.getTime());


let mappedResult1 = mapEngine.map(Number, String, 15);
console.log(mappedResult1);

let mappedResult2 = mapEngine.map(Date, String, new Date());
console.log(mappedResult2);

let mappedResult3 = mapEngine.map(Date, Number, new Date());
console.log(mappedResult3 );

E1: Driving LED’s without resistors

This is part 1 of a 4 part series on the design and creation of a digital dice that can be used to teach children about electronics and/or programming. The design and training material will be available for free once it’s done.

While I was working on this “project”, I questioned everything and I found myself some unexpected answers. Let’s start with these questions and their answers. It allows you to determine whether or not you are interested in this series at all.

  1. Do we always have to use resistors when we use LED’s in battery powered devices?
    Answer: No.
  2. Is it difficult to program your Microchip (fka Atmel) MCU’s without anything Arduino?
    Answer: No.
  3. Can we reduce the standby power consumption of an ATTiny by so much, that it’s cheaper to not add a power button and keep it on standby forever?
    Answer: Yes.
  4. Can I use basic components to create a device, that allows me to measure current draw in nano amps very easily?
    Answer: Yes.

I will use these questions as the guideline for this series. In every post I will try to cover one question and provide you with a more detailed answer.

This week it’s about driving LED’s without resistors.

Context

The device has to be cheap and not contain components that we do not need. If we can do without resistors, I can cut on both the costs and the complexity. Now can we?

Answer

Yes we definitely can, as long as we select the right LED. The device is powered by a CR2032 3V battery. Thus, if we select a type of LED which has a Forward Voltages (Vf) above 3V, we should be safe.

When we look at the following graph from www.electronics-tutorials.ws we can easily see that a green coloured LED would fit our project perfectly.
I/V Characteristics Curves showing the different colours available

The data sheet for the ultra bright green LED that I had lying around and current measurements I did, supported the graph. I never measured more than around 13 mA of current flowing at 3V.

The ATTiny that I had in mind to control the dice can drive 40mA per output pin and a total of 200mA, so we can run 6 LED’s perfectly fine within the specs and without resistors!

That’s it for this week. Next week I’ll cover programming an Atmel chip without Arduino which is a lot easier than I thought.

Blockchain is bad for the environment

How can a technology be bad for the environment you might ask. Well, here’s why.

Blockchain is as safe as it is, because its security is based on raw computing power. Right now there are thousands of computers around the world that are executing complex calculations to ensure validity of transactions worth a few pennies. Ever thought of that? The people or companies that are making money on blockchain technology are actually trading energy and not in an effective environmental friendly way.

Why do we hype stupid technology?