11'use strict' ;
22
3- var _ = require ( '../src/scripts/vendor/minified' ) . _ ;
4- var $ = require ( '../src/scripts/vendor/minified' ) . $ ;
5- var HTML = require ( '../src/scripts/vendor/minified' ) . HTML ;
6- var ClayItem = require ( '../src/scripts/lib/clay-item' ) ;
7- var ClayConfig = require ( '../src/scripts/lib/clay-config' ) ;
8- var Clay = require ( '../index' ) ;
9- var components = require ( '../src/scripts/components' ) ;
10- var componentRegistry = require ( '../src/scripts/lib/component-registry' ) ;
11- var idCounter = 0 ;
3+ import minified = require( '../src/scripts/vendor/minified' ) ;
4+ import createClayItem = require( '../src/scripts/lib/clay-item' ) ;
5+ import createClayConfig = require( '../src/scripts/lib/clay-config' ) ;
6+ import Clay = require( '../index' ) ;
7+ import components = require( '../src/scripts/components' ) ;
8+ import componentRegistry = require( '../src/scripts/lib/component-registry' ) ;
9+ import type { ClayConfigItem , ClayMeta , ClayComponentInput } from '../src/scripts/lib/types' ;
10+
11+ const _ = minified . _ ;
12+ const $ = minified . $ ;
13+ const HTML = minified . HTML ;
14+
15+ let idCounter = 0 ;
1216
1317/**
14- * @param {Object } [extra] - add/replace keys in the meta
15- * @returns {{accountToken: string, watchToken: string, activeWatchInfo: {platform:
16- * string, model: string, language: string, firmware: {major: number, minor:
17- * number, patch: number, suffix: string}}, userData: {} }}
18+ * Create a mock meta object, optionally extended with additional properties.
1819 */
19- module . exports . meta = function ( extra ) {
20- var result = {
20+ function meta ( extra ?: Record < string , unknown > ) : ClayMeta & Record < string , unknown > {
21+ const result : ClayMeta & Record < string , unknown > = {
2122 accountToken : '0123456789abcdef0123456789abcdef' ,
2223 watchToken : '0123456789abcdef0123456789abcdef' ,
2324 activeWatchInfo : {
@@ -34,149 +35,167 @@ module.exports.meta = function(extra) {
3435 userData : { }
3536 } ;
3637
37- _ . eachObj ( extra || { } , function ( key , val ) {
38+ _ . eachObj ( extra || { } , function ( key : string , val : unknown ) {
3839 result [ key ] = val ;
3940 } ) ;
4041
4142 return result ;
42- } ;
43+ }
4344
4445/**
45- * @param {string|Object } config
46- * @param {boolean } [autoRegister=true]
47- * @returns {Clay~ConfigItem }
46+ * Create a config item, with auto-registration if needed.
4847 */
49- module . exports . configItem = function ( config , autoRegister ) {
48+ function configItem ( config : string | ClayConfigItem , autoRegister ?: boolean ) : ClayConfigItem {
49+ let resolvedConfig : ClayConfigItem ;
5050 if ( typeof config === 'string' ) {
51- config = { type : config } ;
51+ resolvedConfig = { type : config } ;
52+ } else {
53+ resolvedConfig = config ;
5254 }
5355
54- var result = _ . extend ( { } , {
55- label : config . type + '-label' ,
56+ const defaults : ClayConfigItem = {
57+ type : resolvedConfig . type ,
58+ label : resolvedConfig . type + '-label' ,
5659 messageKey : 'messageKey-' + idCounter ,
5760 id : 'id-' + idCounter
58- } , config ) ;
61+ } ;
62+ const result : ClayConfigItem = Object . assign ( defaults , resolvedConfig ) ;
5963
6064 idCounter ++ ;
6165
6266 if ( autoRegister !== false &&
63- ! componentRegistry [ result . type ] &&
64- result . type !== 'section' ) {
65- ClayConfig . registerComponent ( components [ result . type ] ) ;
67+ ! ( result . type in componentRegistry ) &&
68+ result . type !== 'section' &&
69+ result . type in components ) {
70+ createClayConfig . registerComponent ( components [ result . type ] ) ;
6671 }
6772
6873 return result ;
69- } ;
74+ }
7075
7176/**
72- * @param {string|Object } config
73- * @param {boolean } [autoRegister=true]
74- * @returns {ClayItem }
77+ * Create a ClayItem from a config.
7578 */
76- module . exports . clayItem = function ( config , autoRegister ) {
77- return new ClayItem ( module . exports . configItem ( config , autoRegister ) ) ;
78- } ;
79+ function clayItem ( config : string | ClayConfigItem , autoRegister ?: boolean ) {
80+ return createClayItem ( configItem ( config , autoRegister ) ) ;
81+ }
7982
8083/**
81- * @param {Array } types
82- * @param {boolean } [autoRegister=true]
83- * @returns {* }
84+ * Create a config array from types, recursively handling nested sections.
8485 */
85- module . exports . config = function ( types , autoRegister ) {
86+ function config ( types : ( string | ClayConfigItem | ( string | ClayConfigItem ) [ ] ) [ ] , autoRegister ?: boolean ) : ClayConfigItem [ ] {
8687 return types . map ( function ( item ) {
87- return Array . isArray ( item ) ?
88- { type : 'section' , items : module . exports . config ( item , autoRegister ) } :
89- module . exports . configItem ( item , autoRegister ) ;
88+ if ( Array . isArray ( item ) ) {
89+ return {
90+ type : 'section' ,
91+ items : config ( item , autoRegister )
92+ } ;
93+ }
94+ return configItem ( item , autoRegister ) ;
9095 } ) ;
91- } ;
96+ }
9297
9398/**
94- * @param {Array } types
95- * @param {boolean } [build=true] - run the build method on the result
96- * @param {boolean } [autoRegister=true]
97- * @param {Object } [settings] - settings to pass to constructor
98- * @param {Object } [meta] - override the meta
99- * @returns {ClayConfig }
99+ * Create a ClayConfig instance from types.
100100 */
101- module . exports . clayConfig = function ( types , build , autoRegister , settings , meta ) {
102- var clayConfig = new ClayConfig (
101+ function clayConfig (
102+ types ?: ( string | ClayConfigItem | ( string | ClayConfigItem ) [ ] ) [ ] ,
103+ build ?: boolean ,
104+ autoRegister ?: boolean ,
105+ settings ?: Record < string , unknown > ,
106+ metaOverride ?: Record < string , unknown >
107+ ) {
108+ const clayConfigInstance = createClayConfig (
103109 settings || { } ,
104- module . exports . config ( types , autoRegister ) ,
105- $ ( HTML ( '<div>' ) ) ,
106- module . exports . meta ( meta )
110+ config ( types || [ ] , autoRegister ) ,
111+ HTML ( '<div>' ) ,
112+ meta ( metaOverride )
107113 ) ;
108- return build === false ? clayConfig : clayConfig . build ( ) ;
109- } ;
114+ return build === false ? clayConfigInstance : clayConfigInstance . build ( ) ;
115+ }
110116
111117/**
112- * @param {Array } config - the Clay config
113- * @param {function } [customFn] - Custom code to run from the config page. Will run
114- * with the ClayConfig instance as context
115- * @param {Object } [options] - Additional options to pass to Clay
116- * @param {boolean } [options.autoHandleEvents] - If false, Clay will not
117- * automatically handle the 'showConfiguration' and 'webviewclosed' events
118- * @param {* } [options.userData={}] - Arbitrary data to pass to the config page. Will
119- * be available as `clayConfig.meta.userData`
120- * @param {boolean } [destroyLocalStorage=true]
121- * @return {Clay }
118+ * Create a Clay instance with optional custom code and options.
122119 */
123- module . exports . clay = function ( config , customFn , options , destroyLocalStorage ) {
120+ function clay (
121+ clayConfig : ClayConfigItem [ ] ,
122+ customFn ?: ( ( this : unknown ) => void ) | null ,
123+ options ?: Record < string , unknown > ,
124+ destroyLocalStorage ?: boolean
125+ ) {
124126 if ( destroyLocalStorage !== false ) {
125127 localStorage . removeItem ( 'clay-settings' ) ;
126128 }
127- return new Clay ( config , customFn , options ) ;
128- } ;
129+ return Clay ( clayConfig , customFn , options ) ;
130+ }
129131
130132/**
131- * @param { Array } keys
132- * @return { Object }
133+ * Convert an array of message key identifiers to a map of key names to IDs.
134+ * Handles array notation like 'myKey[3]' to reserve space for arrays.
133135 */
134- module . exports . messageKeys = function ( keys ) {
135- var counter = 10000 ;
136- var result = { } ;
136+ function messageKeys ( keys : string [ ] ) : Record < string , number > {
137+ let counter = 10000 ;
138+ const result : Record < string , number > = { } ;
137139
138140 keys . forEach ( function ( key ) {
139- var matches = key . match ( / ( .+ ?) (?: \[ ( \d * ) \] ) ? $ / ) ;
140- var parsedKey = matches [ 1 ] ;
141- var length = parseInt ( matches [ 2 ] || 1 , 10 ) ;
141+ const matches = key . match ( / ( .+ ?) (?: \[ ( \d * ) \] ) ? $ / ) ;
142+ if ( ! matches ) {
143+ return ;
144+ }
145+ const parsedKey = matches [ 1 ] ;
146+ const length = parseInt ( matches [ 2 ] || '1' , 10 ) ;
142147
143148 result [ parsedKey ] = counter ;
144149
145150 counter += length ;
146151 } ) ;
147152
148153 return result ;
149- } ;
154+ }
150155
151156/**
152- * @param {Object } obj
153- * @returns {Array }
157+ * Convert an object with arrays to message key notation.
154158 */
155- module . exports . messageKeysObjToArray = function ( obj ) {
159+ function messageKeysObjToArray ( obj : Record < string , unknown > ) : string [ ] {
156160 return Object . keys ( obj ) . map ( function ( key ) {
157- return Array . isArray ( obj [ key ] ) ?
158- key + '[' + obj [ key ] . length + ']' :
161+ const val = obj [ key ] ;
162+ return Array . isArray ( val ) ?
163+ key + '[' + val . length + ']' :
159164 key ;
160165 } ) ;
161- } ;
166+ }
162167
163168/**
164- * @param {Object } expected
165- * @return {Object }
169+ * Convert expected values object to message key map.
166170 */
167- module . exports . messageKeysExpected = function ( expected ) {
168- var result = { } ;
169- var messageKeys = module . exports . messageKeysObjToArray ( expected ) ;
170- messageKeys = module . exports . messageKeys ( messageKeys ) ;
171+ function messageKeysExpected ( expected : Record < string , unknown > ) : Record < string , unknown > {
172+ const result : Record < string , unknown > = { } ;
173+ let messageKeysArray = messageKeysObjToArray ( expected ) ;
174+ const messageKeysMap = messageKeys ( messageKeysArray ) ;
171175
172176 Object . keys ( expected ) . forEach ( function ( key ) {
173- var expectedVal = Array . isArray ( expected [ key ] ) ? expected [ key ] : [ expected [ key ] ] ;
177+ const expectedVal = Array . isArray ( expected [ key ] ) ? expected [ key ] : [ expected [ key ] ] ;
178+ if ( ! Array . isArray ( expectedVal ) ) {
179+ return ;
180+ }
174181 expectedVal . forEach ( function ( val , index ) {
175182 if ( typeof val !== 'undefined' ) {
176- result [ messageKeys [ key ] + index ] = val ;
183+ result [ messageKeysMap [ key ] + index ] = val ;
177184 }
178185 } ) ;
179186 } ) ;
180187
181188 return result ;
189+ }
190+
191+ export = {
192+ meta,
193+ configItem,
194+ clayItem,
195+ config,
196+ clayConfig,
197+ clay,
198+ messageKeys,
199+ messageKeysObjToArray,
200+ messageKeysExpected
182201} ;
0 commit comments