0.1.1 • Published 6 years ago

xtypejs-extension-autocamel-name-scheme v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

xtypejs Extension - Auto Camel Name Scheme

This extension provides a virtual name scheme named auto-camel, which when is the active name scheme, automatically assigns a camel-cased type name to all types in xtypejs, including all default and custom types.

This happens automatically, so that even custom types registered in xtypejs after the extension has already been applied to xtypejs will also each automatically acquire a camel-cased version of their respective type names in the auto-camel name scheme. This is the primary difference between the auto-camel name scheme provided by this extension, and the regular xtypejs camel name scheme module, which only provides a static set of camel-cased type names for the xtypejs built-in types.

Note that this is an xtypejs extension, not an xtypejs name scheme. It therefore requires registration to xtypejs using the xtype.ext.registerExtension method, and cannot be registered using the xtype.ext.registerNameScheme method.

Installation with npm

npm install xtypejs-extension-autocamel-name-scheme --save

NodeJs import and setup

var xtype = require('xtypejs');
var xtypejsAutoCamelNameSchemeExtension = require('xtypejs-extension-autocamel-name-scheme');

xtype.ext.registerExtension(xtypejsAutoCamelNameSchemeExtension);
xtype.options.setNameScheme('auto-camel');

/*
 * Scheme is now active here, and all default and custom types
 * in xtype will now automatically use a camel-converted
 * version of their registered names. For instance, for the
 * 'positive_number' type, the camel name 'positiveNumber'
 * should be used when referencing it in type expressions.
 */

HTML script tag import and setup

Include the extension script after the xtypejs script to automatically register the extension into xtypejs without exporting any globals. This relies on xtypejs being available in the xtype global variable.

<script src="path/to/xtype.js"></script>
<script src="path/to/xtypejs-extension-autocamel-name-scheme.js"></script>

<script>
    xtype.options.setNameScheme('auto-camel');
    // Scheme is now active here
</script>

If the extension script needs to be included before the xtypejs script, or the xtype global is not available (or is using a different variable name) when the extension script is included, the extension will be exported instead to a global variable named xtypejsAutoCamelNameSchemeExtension, which must then be manually registered into xtypejs as an extension.

<!--
    Assume 'xtype' global variable not available here. The following
    exports global variable 'xtypejsAutoCamelNameSchemeExtension'
-->
<script src="path/to/xtypejs-extension-autocamel-name-scheme.js"></script>

<!-- Other things here... -->

<script>
    // Assume xtypejs later available here in variable 'myXtype'
    myXtype.ext.registerExtension(xtypejsAutoCamelNameSchemeExtension);
    myXtype.options.setNameScheme('auto-camel');

    // Scheme is now active here
</script>

Usage

When the auto-camel name scheme is the active scheme, all types, including newly registered custom types, will automatically use a camel-version of the name of the type.

Example:

xtype.options.setNameScheme('auto-camel');

// Registration of custom type 'app_flag' automatically
// uses the camel version of the name ('appFlag') instead:

xtype.registerType('app_flag', 'positive_integer, single_char_string');
xtype.which('g', 'appFlag') === 'appFlag';    // 'app_flag' type uses camel name 'appFlag'

For other related documentation for working with name schemes, also see:

Preventing name collisions

If the HTML script tag was used to import the extension script in a browser environment and in the absence of xtypejs in the xtype global variable, the extension will be exported to a global variable named xtypejsAutoCamelNameSchemeExtension. The noConflict method of the exported extension can be used to reassign the extension to a different namespace or variable name, and return the global xtypejsAutoCamelNameSchemeExtension variable to its previous value prior to including the extension script.

var myAutoCamel = xtypejsAutoCamelNameSchemeExtension.noConflict();

/*
 * myAutoCamel is now xtypejsAutoCamelNameSchemeExtension, while 
 * xtypejsAutoCamelNameSchemeExtension variable is now returned to
 * its original value prior to inclusion of the extension script.
 */ 

Type names used for built-in types


Type Default NameName in Scheme
nullnull
undefinedundefined
nannan
symbolsymbol
functionfunction
datedate
errorerror
regexpregexp
booleanboolean
truetrue
falsefalse
stringstring
whitespacewhitespace
single_char_stringsingleCharString
multi_char_stringmultiCharString
empty_stringemptyString
blank_stringblankString
non_empty_stringnonEmptyString
non_blank_stringnonBlankString
numbernumber
positive_numberpositiveNumber
negative_numbernegativeNumber
zerozero
non_positive_numbernonPositiveNumber
non_negative_numbernonNegativeNumber
non_zero_numbernonZeroNumber
integerinteger
positive_integerpositiveInteger
negative_integernegativeInteger
floatfloat
positive_floatpositiveFloat
negative_floatnegativeFloat
infinite_numberinfiniteNumber
positive_infinitypositiveInfinity
negative_infinitynegativeInfinity
non_infinite_numbernonInfiniteNumber
arrayarray
empty_arrayemptyArray
single_elem_arraysingleElemArray
multi_elem_arraymultiElemArray
non_empty_arraynonEmptyArray
objectobject
empty_objectemptyObject
single_prop_objectsinglePropObject
multi_prop_objectmultiPropObject
non_empty_objectnonEmptyObject
primitiveprimitive
nothingnothing
anyany
nonenone