Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kfly8/type-equal
type constraints for single value equality
https://github.com/kfly8/type-equal
perl
Last synced: 11 days ago
JSON representation
type constraints for single value equality
- Host: GitHub
- URL: https://github.com/kfly8/type-equal
- Owner: kfly8
- License: other
- Created: 2023-08-13T14:29:34.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-17T14:14:25.000Z (8 months ago)
- Last Synced: 2024-12-19T05:11:14.835Z (about 1 month ago)
- Topics: perl
- Language: Perl
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changes
- License: LICENSE
Awesome Lists containing this project
README
[![Actions Status](https://github.com/kfly8/Type-Equal/actions/workflows/test.yml/badge.svg)](https://github.com/kfly8/Type-Equal/actions) [![Coverage Status](https://img.shields.io/coveralls/kfly8/Type-Equal/main.svg?style=flat)](https://coveralls.io/r/kfly8/Type-Equal?branch=main) [![MetaCPAN Release](https://badge.fury.io/pl/Types-Equal.svg)](https://metacpan.org/release/Types-Equal)
# NAMETypes::Equal - type constraints for single value equality
# SYNOPSIS
```perl
use Types::Equal qw( Eq Equ );
use Types::Standard -types;
use Type::Utils qw( match_on_type );# Check single string equality
my $Foo = Eq['foo'];
$Foo->check('foo'); # true
$Foo->check('bar'); # falseeval { Eq[undef]; };
ok $@; # dies# Check single string equality with undefined
my $Bar = Equ['bar'];
$Bar->check('bar'); # truemy $Undef = Equ[undef];
$Undef->check(undef);# Can combine with other types
my $Baz = Eq['baz'];
my $ListBaz = ArrayRef[$Baz];
my $Type = $ListBaz | $Baz;$Type->check(['baz']); # true
$Type->check('baz'); # true# Easily use pattern matching
my $Publish = Eq['publish'];
my $Draft = Eq['draft'];my $post = {
status => 'publish',
title => 'Hello World',
};match_on_type($post->{status},
$Publish => sub { "Publish!" },
$Draft => sub { "Draft..." },
) # => Publish!;# Create simple Algebraic Data Types(ADT)
my $LoginUser = Dict[
_type => Eq['LoginUser'],
id => Int,
name => Str,
];my $Guest = Dict[
_type => Eq['Guest'],
name => Str,
];my $User = $LoginUser | $Guest;
my $user = { _type => 'Guest', name => 'ken' };
$User->assert_valid($user);match_on_type($user,
$LoginUser => sub { "You are LoginUser!" },
$Guest => sub { "You are Guest!" },
) # => 'You are Guest!';
```# DESCRIPTION
Types::Equal provides type constraints for single string equality like TypeScript's string literal types.
## Eq
`Eq` is function of a type constraint [Type::Tiny::Eq](https://metacpan.org/pod/Type%3A%3ATiny%3A%3AEq) which is for single string equality.
## Equ
`Equ` is function of a type constraint [Type::Tiny::Equ](https://metacpan.org/pod/Type%3A%3ATiny%3A%3AEqu) which is for single string equality with undefined.
## NumEq
`NumEq` is function of a type constraint [Type::Tiny::NumEq](https://metacpan.org/pod/Type%3A%3ATiny%3A%3ANumEq) which is for single number equality.
## NumEqu
`NumEqu` is function of a type constraint [Type::Tiny::NumEqu](https://metacpan.org/pod/Type%3A%3ATiny%3A%3ANumEqu) which is for single number equality with undefined.
# LICENSE
Copyright (C) kobaken.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.# AUTHOR
kobaken