Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ember-computed-convert-unit

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-computed-convert-unit

Use convert unit as computed property or decorator

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
2
Weekly downloads
 
Created
Source

ember-computed-convert-unit

Enables convert unit as a computed property

Compatibility

ember-computed-convert-unit is made for ember 3.12 and above, but probably works for older versions too.

Installation

ember install ember-computed-convert-unit

Usage

convertUnit is a computed property that you can use in the same way as you normally would do. It works for both 'classic' Ember and Ember Octane. To indicate a unit, you can use both the full name and the abbreviation of a unit. (e.g. 'kg'/'kilograms', 'ft'/'feet')

import Model       from '@ember-data/model';
import convertUnit from 'ember-computed-convert-unit'

export default Model.extend({
  lengthInMilimeters:  attr('number'),
  lengthInCentimeters: convertUnit('lengthInMilimeters', 'milimeters', 'centimeters')
})
import Model       from '@ember-data/model';
import convertUnit from 'ember-computed-convert-unit'

export default class Paper extends Model {
  @attr('number')
  lengthInMilimeters;

  @convertUnit('lengthInMilimeters', 'milimeters', 'centimeters')
  lengthInCentimeters;
}

Available conversions

This addon uses convert-units to perform the actual conversion. Check its documentation for available conversions.

Custom convertions

It's possible to add (or "overwrite") a conversion as shown in the example below. If you add a conversion for a type that's included in the library, you must use the abbreviation. In the example below feet/ft already exists as a type and TEU doesn't.

// config/enviroment.js
module.exports = function(environment) {
  let ENV = {
    computedConvertUnit: {
      customConversions: [
        {
          from: 'ft',
          to:   'TEU',
          convert(value) {
            return value / 20;
          }
        },
        {
          from: 'TEU',
          to:   'ft',
          convert(value) {
            return value * 20;
          }
        }
      ]
    }
  }
}

// models/rock.js
import Model       from '@ember-data/model';
import convertUnit from 'ember-computed-convert-unit'

export default class Rock extends Model {
  @attr('number')
  lengthInFeet;

  @convertUnit('lengthInMilimeters', 'feet', 'TEU')
  volumeInTeu;
}

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

Keywords

FAQs

Package last updated on 03 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc