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

backbone-attribute-types

v0.1.7

Published

A non-obtrusive plugin that extends models to force `get()`ing of attributes to return values in a specific type (integer, date, etc).

Downloads

3

Readme

Backbone Attribute Types

Version 0.1.7

A non-obtrusive plugin that extends models to force get()ing of attributes to return values in a specific type (integer, date, etc).

Example Usage

var TypedModel = Backbone.Model.extend({

	attrTypes: {
		'count': 'int',
		'runtime': 'float',
		'created_at': 'moment'
	}

});

var typedModel = new TypedModel({
	count: '10',
	runtime: '4.75',
	created_at: '2017-09-19'
})

typedModel.get('count') // 10
typedModel.get('runtime') // 4.75
typedModel.get('created_at') // date object

// a use-case
typedModel.attributes['count'] + 9 // = "109" (this would normally happen)
typedModel.get('count') + 9 // = 19

Attribute Types attrTypes

There are default attribute types under Backbone.ModelAttrTypes. You can add to this or specify a custom type on the model.

attrTypes can also be defined on the Collection.

Supported Types

window.Backbone.ModelAttrTypes = {
	'string': function(val){ return String(val) },
	'json': function(val){ return _.isString(val) ? JSON.parse(val) : val },
	'bool': function(val){ return !!val },
	'int': function(val){ return parseInt(val) },
	'float': function(val){ return parseFloat(val) },
	'num': function(val){ return parseFloat(val) }, // alias for float
	'date': function(val){ return new Date(val) },
	// moment.js support
	'moment': function(val){ return window.moment ? moment(val) : new Date(val) }
}

Custom Types

var CustomTypedModel = Backbone.Model.extend({

	attrTypes: {
		'attrName': function(val){
			// convert val to type
			return val
		}
	}

});

Casting to type

You can cast an attribute to a type on the fly by appending a pipe | and the type at the end of the attribute name.

typedModel.get('runtime') // 4.75
typedModel.get('runtime|int') // 4
typedModel.get('runtime|string') // "4.75"

You can also cast as raw to get the original value stored in attributes

typedModel.get('runtime|raw') // "4.75"

Why?

The reason for this plugin is for when your model has an attribute in a string format (for JSON and database consistency) but you wish to use the value in code as a certain type. Rather than 1) continually converting the value to your desired type or 2) creating a secondary method to make this conversion, this plugin will do it for you.

It's designed to be non-obtrusive by refraining from overwriting the real value in attributes and allows you to continue using .get(attr) like normal.

Changelog

v0.1.3

  • a predefined "json" type

v0.1.2

  • convert all values regardless if they are falsey

License

MIT © Kevin Jantzer