HNHacker News
TopNewBestAskShowJobs

lucono

20 karma · joined December 26, 2014

submissionscomments
lucono··on Eclipse Ceylon
> The idea is that variable is something you will rarely type

This, precisely. I feel Ceylon uses more sensible defaults all around, like also having non-public methods/properties by default unless explicitly declared public. I use Kotlin in some projects, but don't really like its default of public properties and methods unless explicitly declared private, nor their decision on final classes by default.

It's also so many other little things in Ceylon that really add up to make it a pleasure to use, like its simplification of numeric types to just the minimal meaningful set of Integer and Float[1] (no Double, Long, etc), its integer literals which use the metric suffix system, like 5k for 5000, 5M for 5 million, and even hex integer literals like #ffffff, etc.[2][3], its comparison operations which return one of the enumerated Comparison type of smaller, equal, or larger (and not -1, 0, 1) ...etc

[1] https://ceylon-lang.org/documentation/current/tour/language-...

[2] https://ceylon-lang.org/documentation/current/reference/lite...

[3] https://ceylon-lang.org/documentation/current/tour/language-...

lucono··on Xtypejs: data validation for JavaScript Apps
There is a non-open-source JavaScript decorator library using this. It has many (real) examples, but the one that first comes to mind is its use of the xtype.is API method (http://xtype.js.org/api/is) to perform runtime validations based on validation meta-data that's defined in configuration.

For an idea of what that looks like, see:

http://xtype.js.org/guide/checking_combinations_of_types

lucono··on Xtypejs: data validation for JavaScript Apps
Example of using xtypejs:

Go from this:

    function searchEmployees(value) {
        if (typeof value === 'string') {
             if (value.trim().length > 1) {
                return EmployeeDB.searchByName(value);
            } else if (value.trim().length === 1) {
                return EmployeeDB.searchByMiddleInitial(value);
            } else {
                return { error: 'Invalid search value supplied' };
            }
        } else if (typeof value === 'object' && value !== null) {
            if (Object.keys(value).length === 1) {
                return EmployeeDB.searchByFieldValuePair(value);
            } else if (Object.keys(value).length > 1) {
                return { error: 'Search by multiple fields not supported' };
            } else {
                return { error: 'Invalid search value supplied' };
            }
        } else if (typeof value === 'number') {
            if (!isNaN(value) && isFinite(value) && value > 0 && value % 1 === 0) {
                return EmployeeDB.searchByEmployeeNumber(value);
            } else {
                return { error: 'Invalid employee number supplied' };
            }
        } else if (typeof value === 'undefined' || value === null) {
            return { error: 'No search value supplied' };
        } else {
            return { error: 'Invalid search value supplied' };
        }
    }
    
To concise, performant, readable, data validation:

    function searchEmployees(value) {
        switch (xtype.which(value, 'str2+ str1 int+ obj1 obj2+ num nil')) {
            case 'str2+':
                return EmployeeDB.searchByName(value);
            case 'str1':
                return EmployeeDB.searchByMiddleInitial(value);
            case 'int+':
                return EmployeeDB.searchByEmployeeNumber(value);
            case 'obj1':
                return EmployeeDB.searchByFieldValuePair(value);
            case 'obj2+':
                return { error: 'Search by multiple fields not supported' };
            case 'num':
                return { error: 'Invalid employee number supplied' };
            case 'nil':
                return { error: 'No search value supplied' };
            default:
                return { error: 'Invalid search value supplied' };
        }
    }
lucono··on Xtypejs: data validation for JavaScript Apps
Thanks to lots of great community feedback and input, xtypejs now has a home at [http://xtype.js.org](http://xtype.js.org), with great documentation, user guide, and lots of examples.
lucono··on Show HN: Xtypejs – Efficient data-validating pseudo types for JavaScript
Constructive criticism is very welcome. I updated the docs to better highlight the compact naming scheme switch. Definitely appreciate the feedback.
lucono··on Show HN: Xtypejs – Efficient data-validating pseudo types for JavaScript
Thanks. I developed and used it as a utility in a bigger library that worked heavily with types, but later exported it to its own project so I could share it, after I realized how useful it was, even stand-alone.

It is very highly optimized, as performance was of critical importance.

lucono··on Show HN: Xtypejs – Efficient data-validating pseudo types for JavaScript
There is no pseudo-code in the documentation. All of the documentation code executes as documented.

The library works with type names in two naming modes - default and compact. The default name scheme uses the full type names, such as 'positive_integer', while the compact name scheme uses compact names, such as 'int+' for 'positive_integer'.

You can only be in one mode or the other at any one time. You can switch between the two using the xtype.setOptions method:

    xtype(5);    // 'positive_integer'
    
    // Switch to compact name scheme:
    xtype.setOptions({nameScheme:'compact'});
    
    xtype(5);    // 'int+'
    
See this jsbin I've made for this specific piece of code - http://jsbin.com/kixuko/1/edit?html,js,console.

And the documentation for switching between the default and compact name schemes - https://github.com/lucono/xtypejs/blob/master/docs/Usage.md#....