UNPKG

2.21 kBJavaScriptView Raw
1"use strict";
2
3var Util = require("./boilerplate.util");
4
5module.exports = function( cls ) {
6 /**
7 * @member parseConverter
8 * @param {object} type
9 * `unit`
10 * `{float() 0}`
11 * `{boolean2string() CORRECT WRONG}`
12 * `{Behind myConverter}`
13 * `{| neg abs [integer 0]}`
14 * `[yes no maybe]`
15 */
16 cls.prototype.parseConverter = parseConverter;
17 return cls;
18};
19
20
21function parseConverter( type ) {
22 if( typeof type === 'string' ) return convString.call( this, type );
23 if( Array.isArray( type ) ) return convArray.call( this, type );
24 if( Util.isSpecial( type, "const" ) ) return convConst.call( this, type );
25 if( Util.isSpecial( type, "behind" ) ) return convBehind.call( this, type );
26 if( Util.isSpecial( type, "|" ) ) return convPipe.call( this, type );
27 if( Util.isSpecial( type, "*" ) ) return convArgs.call( this, type );
28 throw "Bad converter syntax: " + JSON.stringify( type );
29};
30
31/**
32 * convert: {Const Hello!}
33 */
34function convConst( type ) {
35
36}
37
38
39function convString( type ) {
40 if( CONVERTERS.args0.indexOf( type ) === -1 ) {
41 throw "Unknown simple converter `" + type + "`!\nAvailable simple converters are: "
42 + CONVERTERS.args0.join(", ") + ".";
43 }
44
45 var name = `conv_${type}`;
46 if( this.addName( name ) ) {
47 this.sections.converters.push(
48 `var ${name} = Converters.get('${type}');`
49 );
50 }
51 this.sections.requires.Converters = "require('tfw.binding.converters');";
52 return name;
53}
54
55function convArray( type ) {
56 var name = this.getFreeName( "conv_enum" );
57 this.sections.converters.push(
58 `var ${name} = Converters.get('enum')(${JSON.stringify( type )});`
59 );
60 this.sections.requires.Converters = "require('tfw.binding.converters');";
61 return name;
62}
63
64function convBehind( type ) {
65
66}
67
68function convPipe( type ) {
69}
70
71function convArgs( type ) {
72}
73
74
75var CONVERTERS = {
76 args0: [
77 "array",
78 "boolean",
79 "booleans",
80 "color",
81 "intl",
82 "isEmpty",
83 "isNotEmpty",
84 "keys",
85 "length",
86 "list",
87 "multilang",
88 "not",
89 "sortedKeys",
90 "string",
91 "strings",
92 "unit",
93 "units",
94 "validator"
95 ]
96};