npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

domain-properties

v2.0.5

Published

Allow the use of domain specific classes as wrappers of primitive values.

Downloads

10

Readme

This project has been moved to ng-domain-prop. Although you can still download it here, it will only be updated there.

The domain-project, is an angular library that allows to work with domain specific types as inputs. It allows to work with the domain type much like they were javascript primitive types.

	Class CommaNumber extends DomainValue<number>{
		//...Validation and conversion stuff
	}

	@UsesDomainValues
	Class MyDomainClass {
	@DomainProperty(CommaNumber) value:any = 5; //CommaNumber type behave much like a number although is really a CommaNumber object.

		addSomeValue(ammount:number){
			this.value = '3,6'; //set attribute from string;
			this.value += number;  //Its possible to do math with the property.
		}
	}

	@Component({
		template: `<input [(ngMpodel)]='value' />`
	})
	export class MyComponent{
		private value:CommaNumber; //input from user will be converted as defined in the domain type
	}

It also allows to configure angular forms to use form validation based on DomainTypes:

	@ValidateDomainValues
	@Component({
		template: `<input [(ngMpodel)]='value' />`
	})
	export class MyComponent{
		private value:MyDomainType; /*the input will be validated by the domain class.*/
	}

##Usage:

First, the domain specific type must extends the DomainValue class:

	abstract class DomainValue<PRIMITIVE_TYPE>{
		protected value:PRIMITIVE_TYPE; 
		constructor(value:PRIMITIVE_TYPE));
		public equals(value:any):boolean;
		public setValue(value:any);
		public getValue();
		public valueOf():PRIMITIVE_TYPE;
		public isValid(value:string):boolean;
		protected primitiveTomPrimitive(value:PRIMITIVE_TYPE):PRIMITIVE_TYPE;
		protected abstract stringToPrimitive(value:string):PRIMITIVE_TYPE;
		abstract primitiveToString():string;
	}

All the methods have a default implementation and you can override the following methods:

	/**
	* Gets the string value and convert to the apropriate primitive value. Implement in case some conversion is needed.
 	* @param {string} value - The string value to be converted.
 	*/
	protected stringToPrimitive(value:string):PRIMITIVE_TYPE;

	/**
        * Returns the apropriate string representation of the value. Ovewrite in case you the to convert the primitive value before is shown
	* @param {value::PRIMITIVE_TYPE} value - The primitive value to be converted.
        */
	primitiveToString(value::PRIMITIVE_TYPE):string;
	
	/**
        * Convert a primitive value before set the value. Implement in case you need to do some conversion on a primitive value.
        * @param {value::PRIMITIVE_TYPE} value - The primitive value to be converted.
        */
	primitiveToPrimitive(value:value:PRIMITIVE_TYPE)::PRIMITIVE_TYPE //Gets the value from a primitive value, implement the method in case

	/**
        * Determine whether a value is valid or not. 
        * @param {value::string} value - The string value to be validated.
        */
	isValid(value:string):boolean;

Obs: The other methods are part of the public API or are used by the class itself, so we don't recommend overriding them.

####Example:

import {DomainValue} from 'domain-properties/domain-properties';

export class CommaNumber extends DomainValue<number>{

  /*Converts from '00,00' to a valid javascript number */
  protected stringToPrimitivevalue:string):number {
    return parseFloat(value.replace(/,/, '.'));
  }

  /*Converts from number in the format 00.00 to a string in the format '00,00'*/
  public primitiveToString(value:number):string {
     return ("" + value).replace(/\./, ',');
  }

  /*Valid for inputs in the format '00,00'*/
  public isValid(value:string):boolean{
    return /^[0-9]+(,[0-9]+)?$/.test(value);
  }
}

Second, the class containing the domaintype should be correctelly decorated:

	import {UsesDomainProperties, DomainProperty} from 'domain-properties/domain-properties';

	@UsesDomainValues //Tells the lib to configure domain type attributes
	Class MyDomainClass {
		/*Defines that the attribute should be treated as a domain type*/
		@DomainProperty(CommaNumber) value:any = 5; 
	}

Finnaly, if you want to use validationm the component the input thats must be have the 'validate-domain' directive:

	import {ValidateDomainValues} from 'domain-properties/domain-properties';

	@Component({
		template: `<input validate-domain validate-domain [(ngMpodel)]='myClass.value' />`
	})
	export class MyComponent{
		private myClass:MyDomainClass; //the input will be validated by the domain class.
	}

##Update:

In the previous version to use validation you had to decorate the class with @ValidateDomainValues decorator, but now you must use the validate-domain directive instead. The change was made because of two reasons:

  1. I discovered some angular2 behaviour that causes a bug when the input tag is conditionally rendered (ngIf). To set validation the lib uses @ViewChildren to inject the NgControl object. The problem is that @ViewChildren are injected before AfterViewInit event, and that event is fired just once, so if you have some input with ngIf initially not rendered his NgContol is not injected, even if the it changes his state to be rendered.

  2. Using the directive allows to decide which input should be validated. The @ValidateDomainValues decorator is used in the component itself, so if you decorate the component all of its inputs that points to DomainValues objects are autommatically rendered. Using the directive, you can decide which input will be validated.

##Update2:

I started this project initially just to use it myself and to learn how to deploy packages on npm. It seems that other people are downloading, and probably using it, and I am very glad with that. Because this project doesn't have a version number that matches it real state I decided to move the code to another project (ng-domain-prop). That project was published on github so people can contribute to it. Although I will not delete this project I strongly recommed to use the other project, that I will keep updated.