New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

can-type

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

can-type - npm Package Compare versions

Comparing version 0.1.4 to 0.1.5

2

doc/any.md
@property {can-type.typeobject} can-type.any Any
@parent can-type
@parent can-type/types
@description The `Any` type represents any type.

@@ -4,0 +4,0 @@

@page can-type
@parent can-data-validation
@collection can-ecosystem
@description Define type objects that can coerce and check types.
@group can-type/methods 0 methods
@group can-type/types 1 types
@outline 2
@body
## Usage
## Overview

@@ -79,1 +81,81 @@ Use can-type to define type checking rules around types. Works well with [can-define], [can-define-object], and [can-stache-define-element].

```
## Creating Models and ViewModels
can-type is useful for creating typed properties in [can-define-object]. Some DefineObjects you might want to have stricter type checking, others might need to be loose.
```js
import { DefineObject, type } from "can/everything";
class Person extends DefineObject {
static define = {
first: type.check(String),
last: type.maybe(String),
age: type.convert(Number),
birthday: type.maybeConvert(Date)
};
}
let fib = new Person({
first: "Fibonacci",
last: null,
age: "80",
birthday: undefined
});
console.log( fib ); // -> { ... }
```
@codepen
When creating models with [can-rest-model] you might want to be loose in the typing of properties, especially when working with external services you do not have control over.
On the other hand, when creating ViewModels for components, such as with [can-stache-define-element] you might want to be stricter about how properties are passed, to prevent mistakes.
```js
import { StacheDefineElement, type } from "can/everything";
class Progress extends StacheDefineElement {
static define = {
value: {
type: type.check(Number),
default: 0
},
max: {
type: type.check(Number),
default: 100
},
get width() {
let w = (this.value / this.max) * 100;
return w + '%';
}
};
static view = `
<div style="background: black;">
<span style="background: salmon; display: inline-block; width: {{width}}">&nbsp;</span>
</div>
`;
}
customElements.define("custom-progress-bar", Progress);
let progress = new Progress();
progress.value = 34;
document.body.append(progress);
function increment() {
setTimeout(() => {
if(progress.value < 100) {
progress.value++;
increment();
}
}, 500);
}
increment();
```
@codepen
See [can-stache-define-element] and [can-define-object] for more on its API.
@function can-type/check check
@parent can-type
@parent can-type/methods 0
@description Create a strictly typed TypeObject.

@@ -4,0 +4,0 @@

@function can-type/convert convert
@parent can-type
@parent can-type/methods 2
@description Create a coercing [can-type.typeobject].

@@ -4,0 +4,0 @@

@function can-type/maybe maybe
@parent can-type
@parent can-type/methods 1
@description Create a strictly typed TypeObject that also accepts `null` and `undefined` values.

@@ -4,0 +4,0 @@

@function can-type/maybeConvert maybeConvert
@parent can-type
@parent can-type/methods 3
@description Create a converting [can-type.typeobject] that also accepts `null` and `undefined`.

@@ -4,0 +4,0 @@

@typedef {{}} can-type.typeobject TypeObject
@parent can-type
@parent can-type/types
@description An object describing how to test membership for and convert to a specified type.

@@ -4,0 +4,0 @@

{
"name": "can-type",
"version": "0.1.4",
"version": "0.1.5",
"description": "Type definitions",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/canjs/can-type",

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