A string formatter and validator based on masks
Port of https://github.com/the-darc/string-mask to Dart
Please file feature requests and bugs at the [issue tracker][tracker].
A string formatter and validator based on masks.
With pub.dartlang.org!
dependencies:
string_mask: "^0.0.1"
Character | Description |
---|---|
0 | Any numbers |
9 | Any numbers (Optional) |
# | Any numbers (recursive) |
A | Any alphanumeric character |
a | Any alphanumeric character (Optional) Not implemented yet |
S | Any letter |
U | Any letter (All lower case character will be mapped to uppercase) |
L | Any letter (All upper case character will be mapped to lowercase) |
$ | Escape character, used to escape any of the special formatting characters. |
Optional characters: Used to parse characters that cold exist in the source string or not. See Date and time.
Recursive characters: Used to parse patterns that repeat in the end or in the start of the source string. See Two decimal number with thousands separators
Note: Any character of the mask positioned after a recursive character will be handled as a non special character.
Use it creating an mask instance with the StringMask contructor:
/**
* - optionsObject parameter is optional in the constructor
* - apply will return the a masked string value
* - validate will return `true` if the string matchs the mask
*/
var mask = new StringMask('some mask', options: new MaskOptions()); //optionsObject is optional
var maskedValue = mask.apply('some value string');
var isValid = mask.validate('some value string to validate');
Or by the static interface:
/**
* - optionsObject parameter is optional in all methods
* - apply will return the a masked string value
* - validate will return `true` if the string matchs the mask
* - process will return a object: {result: <maskedValue>, valid: <isValid>}
*/
var maskedValue = StringMask.apply_('some value string', 'some mask', optionsObject);
var isValid = StringMask.validate_('some value string', 'some mask', optionsObject);
var result = StringMask.process_('some value string', 'some mask', optionsObject);
var maskOptions = new MaskOptions()
..reverse = true;
var formatter = new StringMask("#0", options: maskOptions);
var result = formatter.apply('123'); // 123
var maskOptions = new MaskOptions()
..reverse = true;
var formatter = new StringMask('#.##0,00', options: maskOptions);
var result = formatter.apply('100123456'); // 1.001.234,56
var result2 = formatter.apply('6'); // 0,06
var formatter = new StringMask('+00 (00) 0000-0000');
var result = formatter.apply('553122222222'); // +55 (31) 2222-2222
var maskOptions = new MaskOptions()
..reverse = true;
var formatter = new StringMask('#.##0,00', options: maskOptions);
var result = formatter.apply('001'); // 0,01%
var formatter = new StringMask('000.000.000-00');
var result = formatter.apply('12965815620'); // 129.658.156-20
var formatter = new StringMask('90/90/9900');
var result = formatter.apply('1187'); // 1/1/87
var formatter = new StringMask('UUUUUUUUUUUUU');
var result = formatter.apply('To Upper Case'); // TO UPPER CASE
var formatter = new StringMask('LLLLLLLLLLLLL');
var result = formatter.apply('To Lower Case'); // to lower case
var formatter = new StringMask('UUAA AAAA AAAA AAAA AAAA AAAA AAA');
var result = formatter.apply('FR761111BBBB69410000AA33222');
// result: FR76 1111 BBBB 6941 0000 AA33 222
We'd love for you to contribute to our source code! We just ask to:
$ gulp pre-push
)$gulp pre-push
taskCopyright (c) 2016 Daniel Campos (JS Project) Copyright (c) 2017 Emerson Moura (DartProject)
Licensed under the MIT license.
example/string_mask_example.dart
// Copyright (c) 2017, EmersonMoura. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.
main() {
}
Add this to your package's pubspec.yaml file:
dependencies:
string_mask: ^0.1.0
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter packages get
Alternatively, your editor might support pub get
or flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:string_mask/string_mask.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.1.0 | Nov 8, 2018 |
|
|
0.0.1 | Aug 20, 2017 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
76
|
Health:
Code health derived from static analysis.
[more]
|
97
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
68
|
Overall:
Weighted score of the above.
[more]
|
81
|
We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter, web, other
No platform restriction found in primary library
package:string_mask/string_mask.dart
.
Fix lib/src/string_mask_base.dart
. (-3.45 points)
Analysis of lib/src/string_mask_base.dart
reported 7 hints, including:
line 149 col 11: Use isEmpty instead of length
line 154 col 11: Use isNotEmpty instead of length
line 161 col 27: Use isNotEmpty instead of length
line 204 col 11: Use isNotEmpty instead of length
line 240 col 38: Use isNotEmpty instead of length
Format lib/src/mask_pattern.dart
.
Run dartfmt
to format lib/src/mask_pattern.dart
.
Homepage URL doesn't exist. (-20 points)
At the time of the analysis the homepage
field https://www.olimou.com
was unreachable.
The package description is too short. (-12 points)
Add more detail to the description
field of pubspec.yaml
. Use 60 to 180 characters to describe the package, what it does, and its target use case.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0 <3.0.0 | ||
Dev dependencies | |||
test | ^1.4.0 |