Best JavaScript code snippet using root
ReservoirSampler.test.ts
Source:ReservoirSampler.test.ts
...19 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 1,20 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);21 assert.ok(sampler.getNumberInstances() === 1,22 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);23 assert.ok(sampler.getInstances().label0.length === 1,24 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);25 }26 sampler.addInstance("label0", "label0 - utterance 1");27 {28 // ==== assert.ok(sampler.getNumberLabels() === 1,29 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);30 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 1,31 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);32 assert.ok(sampler.getNumberInstances() === 2,33 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);34 assert.ok(sampler.getInstances().label0.length === 2,35 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);36 }37 sampler.addInstance("label0", "label0 - utterance 2");38 {39 // ==== assert.ok(sampler.getNumberLabels() === 1,40 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);41 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 1,42 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);43 assert.ok(sampler.getNumberInstances() === 3,44 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);45 assert.ok(sampler.getInstances().label0.length === 3,46 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);47 }48 sampler.addInstance("label0", "label0 - utterance 3");49 {50 // ==== assert.ok(sampler.getNumberLabels() === 1,51 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);52 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 1,53 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);54 assert.ok(sampler.getNumberInstances() === 4,55 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);56 assert.ok(sampler.getInstances().label0.length === 4,57 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);58 }59 sampler.addInstance("label1", "label1 - utterance 0");60 {61 // ==== assert.ok(sampler.getNumberLabels() === 2,62 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);63 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 2,64 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);65 assert.ok(sampler.getNumberInstances() === 5,66 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);67 assert.ok(sampler.getInstances().label0.length === 4,68 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);69 assert.ok(sampler.getInstances().label1.length === 1,70 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);71 }72 sampler.addInstance("label1", "label1 - utterance 1");73 {74 // ==== assert.ok(sampler.getNumberLabels() === 2,75 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);76 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 2,77 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);78 assert.ok(sampler.getNumberInstances() === 6,79 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);80 assert.ok(sampler.getInstances().label0.length === 4,81 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);82 assert.ok(sampler.getInstances().label1.length === 2,83 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);84 }85 sampler.addInstance("label2", "label2 - utterance 0");86 {87 // ==== assert.ok(sampler.getNumberLabels() === 3,88 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);89 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 3,90 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);91 assert.ok(sampler.getNumberInstances() === 7,92 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);93 assert.ok(sampler.getInstances().label0.length === 4,94 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);95 assert.ok(sampler.getInstances().label1.length === 2,96 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);97 assert.ok(sampler.getInstances().label2.length === 1,98 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);99 }100 sampler.addInstance("label2", "label2 - utterance 1");101 {102 // ==== assert.ok(sampler.getNumberLabels() === 3,103 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);104 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 3,105 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);106 assert.ok(sampler.getNumberInstances() === 8,107 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);108 assert.ok(sampler.getInstances().label0.length === 4,109 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);110 assert.ok(sampler.getInstances().label1.length === 2,111 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);112 assert.ok(sampler.getInstances().label2.length === 2,113 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);114 }115 sampler.addInstance("label2", "label2 - utterance 2");116 {117 // ==== assert.ok(sampler.getNumberLabels() === 3,118 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);119 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 3,120 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);121 assert.ok(sampler.getNumberInstances() === 9,122 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);123 assert.ok(sampler.getInstances().label0.length === 4,124 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);125 assert.ok(sampler.getInstances().label1.length === 2,126 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);127 assert.ok(sampler.getInstances().label2.length === 3,128 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);129 }130 sampler.addInstance("label2", "label2 - utterance 3");131 {132 // ==== assert.ok(sampler.getNumberLabels() === 3,133 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);134 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 3,135 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);136 assert.ok(sampler.getNumberInstances() === 10,137 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);138 assert.ok(sampler.getInstances().label0.length === 4,139 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);140 assert.ok(sampler.getInstances().label1.length === 2,141 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);142 assert.ok(sampler.getInstances().label2.length === 4,143 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);144 }145 sampler.addInstance("label2", "label2 - utterance 4");146 {147 // ==== assert.ok(sampler.getNumberLabels() === 3,148 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);149 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 3,150 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);151 assert.ok(sampler.getNumberInstances() === 11,152 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);153 assert.ok(sampler.getInstances().label0.length === 4,154 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);155 assert.ok(sampler.getInstances().label1.length === 2,156 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);157 assert.ok(sampler.getInstances().label2.length === 5,158 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);159 }160 sampler.addInstance("label2", "label2 - utterance 5");161 {162 // ==== assert.ok(sampler.getNumberLabels() === 3,163 // ==== `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);164 // ==== assert.ok(Utility.getMapLength(sampler.getLabelMap()) === 3,165 // ==== `Utility.getMapLength(sampler.getLabelMap())=${Utility.getMapLength(sampler.getLabelMap())}`);166 assert.ok(sampler.getNumberInstances() === 12,167 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);168 assert.ok(sampler.getInstances().label0.length === 4,169 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);170 assert.ok(sampler.getInstances().label1.length === 2,171 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);172 assert.ok(sampler.getInstances().label2.length === 6,173 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);174 }175 });176 it("Test.0100 resetLabelsAndMap()", function() {177 Utility.resetFlagToPrintDebuggingLogToConsole(UnitTestHelper.getDefaultUnitTestDebuggingLogFlag());178 this.timeout(UnitTestHelper.getDefaultUnitTestTimeout());179 const sampler: ReservoirSampler<string> = new ReservoirSampler<string>();180 sampler.addInstance("label0", "label0 - utterance 0");181 sampler.resetLabelsAndMap();182 {183 assert.ok(sampler.getNumberLabels() === 1,184 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);185 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 1,186 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);187 assert.ok(sampler.getNumberInstances() === 1,188 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);189 assert.ok(sampler.getInstances().label0.length === 1,190 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);191 }192 sampler.addInstance("label0", "label0 - utterance 1");193 sampler.resetLabelsAndMap();194 {195 assert.ok(sampler.getNumberLabels() === 1,196 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);197 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 1,198 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);199 assert.ok(sampler.getNumberInstances() === 2,200 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);201 assert.ok(sampler.getInstances().label0.length === 2,202 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);203 }204 sampler.addInstance("label0", "label0 - utterance 2");205 sampler.resetLabelsAndMap();206 {207 assert.ok(sampler.getNumberLabels() === 1,208 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);209 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 1,210 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);211 assert.ok(sampler.getNumberInstances() === 3,212 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);213 assert.ok(sampler.getInstances().label0.length === 3,214 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);215 }216 sampler.addInstance("label0", "label0 - utterance 3");217 sampler.resetLabelsAndMap();218 {219 assert.ok(sampler.getNumberLabels() === 1,220 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);221 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 1,222 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);223 assert.ok(sampler.getNumberInstances() === 4,224 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);225 assert.ok(sampler.getInstances().label0.length === 4,226 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);227 }228 sampler.addInstance("label1", "label1 - utterance 0");229 sampler.resetLabelsAndMap();230 {231 assert.ok(sampler.getNumberLabels() === 2,232 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);233 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 2,234 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);235 assert.ok(sampler.getNumberInstances() === 5,236 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);237 assert.ok(sampler.getInstances().label0.length === 4,238 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);239 assert.ok(sampler.getInstances().label1.length === 1,240 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);241 }242 sampler.addInstance("label1", "label1 - utterance 1");243 sampler.resetLabelsAndMap();244 {245 assert.ok(sampler.getNumberLabels() === 2,246 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);247 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 2,248 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);249 assert.ok(sampler.getNumberInstances() === 6,250 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);251 assert.ok(sampler.getInstances().label0.length === 4,252 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);253 assert.ok(sampler.getInstances().label1.length === 2,254 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);255 }256 sampler.addInstance("label2", "label2 - utterance 0");257 sampler.resetLabelsAndMap();258 {259 assert.ok(sampler.getNumberLabels() === 3,260 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);261 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 3,262 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);263 assert.ok(sampler.getNumberInstances() === 7,264 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);265 assert.ok(sampler.getInstances().label0.length === 4,266 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);267 assert.ok(sampler.getInstances().label1.length === 2,268 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);269 assert.ok(sampler.getInstances().label2.length === 1,270 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);271 }272 sampler.addInstance("label2", "label2 - utterance 1");273 sampler.resetLabelsAndMap();274 {275 assert.ok(sampler.getNumberLabels() === 3,276 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);277 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 3,278 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);279 assert.ok(sampler.getNumberInstances() === 8,280 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);281 assert.ok(sampler.getInstances().label0.length === 4,282 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);283 assert.ok(sampler.getInstances().label1.length === 2,284 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);285 assert.ok(sampler.getInstances().label2.length === 2,286 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);287 }288 sampler.addInstance("label2", "label2 - utterance 2");289 sampler.resetLabelsAndMap();290 {291 assert.ok(sampler.getNumberLabels() === 3,292 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);293 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 3,294 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);295 assert.ok(sampler.getNumberInstances() === 9,296 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);297 assert.ok(sampler.getInstances().label0.length === 4,298 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);299 assert.ok(sampler.getInstances().label1.length === 2,300 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);301 assert.ok(sampler.getInstances().label2.length === 3,302 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);303 }304 sampler.addInstance("label2", "label2 - utterance 3");305 sampler.resetLabelsAndMap();306 {307 assert.ok(sampler.getNumberLabels() === 3,308 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);309 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 3,310 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);311 assert.ok(sampler.getNumberInstances() === 10,312 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);313 assert.ok(sampler.getInstances().label0.length === 4,314 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);315 assert.ok(sampler.getInstances().label1.length === 2,316 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);317 assert.ok(sampler.getInstances().label2.length === 4,318 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);319 }320 sampler.addInstance("label2", "label2 - utterance 4");321 sampler.resetLabelsAndMap();322 {323 assert.ok(sampler.getNumberLabels() === 3,324 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);325 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 3,326 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);327 assert.ok(sampler.getNumberInstances() === 11,328 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);329 assert.ok(sampler.getInstances().label0.length === 4,330 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);331 assert.ok(sampler.getInstances().label1.length === 2,332 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);333 assert.ok(sampler.getInstances().label2.length === 5,334 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);335 }336 sampler.addInstance("label2", "label2 - utterance 5");337 sampler.resetLabelsAndMap();338 {339 assert.ok(sampler.getNumberLabels() === 3,340 `sampler.getNumberLabels()=${sampler.getNumberLabels()}`);341 assert.ok(DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap()) === 3,342 `DictionaryMapUtility.getMapLength(sampler.getLabelMap())=${DictionaryMapUtility.getStringIdGenericValueDictionaryLength(sampler.getLabelMap())}`);343 assert.ok(sampler.getNumberInstances() === 12,344 `sampler.getNumberInstances()=${sampler.getNumberInstances()}`);345 assert.ok(sampler.getInstances().label0.length === 4,346 `sampler.getInstances()["label0"].length=${sampler.getInstances().label0.length}`);347 assert.ok(sampler.getInstances().label1.length === 2,348 `sampler.getInstances()["label1"].length=${sampler.getInstances().label1.length}`);349 assert.ok(sampler.getInstances().label2.length === 6,350 `sampler.getInstances()["label2"].length=${sampler.getInstances().label2.length}`);351 }352 });353 it("Test.0200 getNumberLabels()", function() {354 Utility.resetFlagToPrintDebuggingLogToConsole(UnitTestHelper.getDefaultUnitTestDebuggingLogFlag());355 this.timeout(UnitTestHelper.getDefaultUnitTestTimeout());356 const sampler: ReservoirSampler<string> = new ReservoirSampler<string>();357 sampler.addInstance("label0", "label0 - utterance 0");358 const numberLabels: number = sampler.getNumberLabels();359 Utility.debuggingLog(360 `numberLabels=${numberLabels}`);361 });362 it("Test.0201 getLabels()", function() {363 Utility.resetFlagToPrintDebuggingLogToConsole(UnitTestHelper.getDefaultUnitTestDebuggingLogFlag());364 this.timeout(UnitTestHelper.getDefaultUnitTestTimeout());365 const sampler: ReservoirSampler<string> = new ReservoirSampler<string>();366 sampler.addInstance("label0", "label0 - utterance 0");367 const labels: string[] = sampler.getLabels();368 Utility.debuggingLog(369 `labels=${labels}`);370 });371 it("Test.0202 getLabelMap()", function() {372 Utility.resetFlagToPrintDebuggingLogToConsole(UnitTestHelper.getDefaultUnitTestDebuggingLogFlag());373 this.timeout(UnitTestHelper.getDefaultUnitTestTimeout());374 const sampler: ReservoirSampler<string> = new ReservoirSampler<string>();375 sampler.addInstance("label0", "label0 - utterance 0");376 const labelMap: { [id: string]: number } = sampler.getLabelMap();377 Utility.debuggingLog(378 `labelMap=${labelMap}`);379 });380 it("Test.0203 getInstances()", function() {381 Utility.resetFlagToPrintDebuggingLogToConsole(UnitTestHelper.getDefaultUnitTestDebuggingLogFlag());382 this.timeout(UnitTestHelper.getDefaultUnitTestTimeout());383 const sampler: ReservoirSampler<string> = new ReservoirSampler<string>();384 sampler.addInstance("label0", "label0 - utterance 0");385 const instances: { [id: string]: string[] } = sampler.getInstances();386 Utility.debuggingLog(387 `instances=${instances}`);388 });389 it("Test.0300 getNumberInstances()", function() {390 Utility.resetFlagToPrintDebuggingLogToConsole(UnitTestHelper.getDefaultUnitTestDebuggingLogFlag());391 this.timeout(UnitTestHelper.getDefaultUnitTestTimeout());392 const sampler: ReservoirSampler<string> = new ReservoirSampler<string>();393 sampler.addInstance("label0", "label0 - utterance 0");394 const numberInstances: number = sampler.getNumberInstances();395 Utility.debuggingLog(396 `numberInstances=${numberInstances}`);397 });398 it("Test.0500 sampleInstances()", function() {399 Utility.resetFlagToPrintDebuggingLogToConsole(UnitTestHelper.getDefaultUnitTestDebuggingLogFlag());400 this.timeout(UnitTestHelper.getDefaultUnitTestTimeout());401 const sampler: ReservoirSampler<string> = new ReservoirSampler<string>();402 sampler.addInstance("label0", "label0 - utterance 0");403 sampler.addInstance("label0", "label0 - utterance 1");404 sampler.addInstance("label0", "label0 - utterance 2");405 sampler.addInstance("label0", "label0 - utterance 3");406 sampler.addInstance("label1", "label1 - utterance 0");407 sampler.addInstance("label1", "label1 - utterance 1");408 sampler.addInstance("label2", "label2 - utterance 0");409 sampler.addInstance("label2", "label2 - utterance 1");410 sampler.addInstance("label2", "label2 - utterance 2");411 sampler.addInstance("label2", "label2 - utterance 3");412 sampler.addInstance("label2", "label2 - utterance 4");413 sampler.addInstance("label2", "label2 - utterance 5");414 sampler.resetLabelsAndMap();415 Utility.debuggingLog(416 sampler.getInstances());417 Utility.debuggingLog(418 Object.keys(sampler.getInstances()));419 Utility.debuggingLog(420 "sampler.getNumberInstances() = " + sampler.getNumberInstances());421 Utility.debuggingLog(422 "sampler.getNumberLabels() = " + sampler.getNumberLabels());423 Utility.debuggingLog(424 sampler.getLabels());425 Utility.debuggingLog(426 sampler.getLabelMap());427 Utility.debuggingLog(428 sampler.getInstances());429 const sampledInstances = sampler.sampleInstances(6, 2);430 for (const instance of sampledInstances) {431 Utility.debuggingLog(432 instance);433 }434 });...
code.js
Source:code.js
1figma.showUI(__html__, { width: 400, height: 610 });2async function findAllFonts() {3 const nodes = [];4 figma.currentPage5 .findAll((node) => node.type === "TEXT")6 .forEach((node) => nodes.push(node.fontName !== figma.mixed && node.fontName.family));7 const uniqueFonts = [...new Set(nodes)];8 const selectList = uniqueFonts.filter((val) => val !== false);9 figma.ui.postMessage({ fontNames: selectList });10}11findAllFonts();12figma.ui.onmessage = (msg) => {13 if (msg.type === "get-instances-by-name") {14 let nodeName;15 let getInstances;16 const { selection } = figma.currentPage;17 function getInstanceByName() {18 nodeName = selection[0].name;19 }20 getInstanceByName();21 getInstances = figma.currentPage22 .findAll()23 .filter((node) => node.name === nodeName);24 figma.currentPage.selection = getInstances;25 figma.notify(`${getInstances.length} "${nodeName}'s" selected`);26 }27 if (msg.type === "get-instances-by-type") {28 let nodeType;29 let getInstances;30 const { selection } = figma.currentPage;31 function getInstanceByType() {32 nodeType = selection[0].type;33 }34 getInstanceByType();35 getInstances = figma.currentPage36 .findAll()37 .filter((node) => node.type === nodeType);38 figma.currentPage.selection = getInstances;39 figma.notify(`${getInstances.length} "${nodeType}'s" selected`);40 }41 if (msg.type === "get-instances-by-size") {42 let nodeWidth;43 let nodeHeight;44 let getInstances;45 const { selection } = figma.currentPage;46 function getInstanceBySize() {47 nodeWidth = selection[0].width;48 nodeHeight = selection[0].height;49 }50 getInstanceBySize();51 getInstances = figma.currentPage52 .findAll()53 .filter((node) => node.width === nodeWidth && node.height === nodeHeight);54 figma.currentPage.selection = getInstances;55 figma.notify(`${getInstances.length} nodes selected`);56 }57 if (msg.type === "get-instances-by-width") {58 let nodeWidth;59 let getInstances;60 const { selection } = figma.currentPage;61 function getInstanceByWidth() {62 nodeWidth = selection[0].width;63 }64 getInstanceByWidth();65 getInstances = figma.currentPage66 .findAll()67 .filter((node) => node.width === nodeWidth);68 figma.currentPage.selection = getInstances;69 figma.notify(`${getInstances.length} nodes selected`);70 }71 if (msg.type === "get-instances-by-height") {72 let nodeHeight;73 let getInstances;74 const { selection } = figma.currentPage;75 function getInstanceByHeight() {76 nodeHeight = selection[0].height;77 }78 getInstanceByHeight();79 getInstances = figma.currentPage80 .findAll()81 .filter((node) => node.height === nodeHeight);82 figma.currentPage.selection = getInstances;83 figma.notify(`${getInstances.length} nodes selected`);84 }85 if (msg.type === "get-instances-by-radius") {86 let nodeRadius;87 let getInstances;88 let nodetypes = [89 "FRAME",90 "COMPONENT",91 "INSTANCE",92 "GROUP",93 "VECTOR",94 "LINE",95 "STAR",96 "ELLIPSE",97 "RECTANGLE",98 "POLYGON",99 ];100 const { selection } = figma.currentPage;101 function getInstanceByRadius(node) {102 nodetypes.includes(node.type) ? (nodeRadius = node.cornerRadius) : null;103 }104 getInstanceByRadius(selection[0]);105 getInstances = figma.currentPage106 .findAll()107 .filter((node) => nodetypes.includes(node.type) && node.cornerRadius === nodeRadius);108 figma.currentPage.selection = getInstances;109 figma.notify(`${getInstances.length} nodes selected`);110 }111 if (msg.type === "get-instances-by-fills") {112 if (figma.currentPage.selection.length === 1) {113 let getInstances;114 const { selection } = figma.currentPage;115 const selectionFills = selection[0].fills;116 getInstances = figma.currentPage117 .findAll((node) => node.fills[0].color !== undefined)118 .filter((node) => node.fills[0].color.r === selectionFills[0].color.r &&119 node.fills[0].color.g === selectionFills[0].color.g &&120 node.fills[0].color.b === selectionFills[0].color.b);121 figma.currentPage.selection = getInstances;122 figma.notify(`${getInstances.length} fill nodes selected`);123 }124 }125 if (msg.type === "get-instances-by-fillType") {126 if (figma.currentPage.selection.length === 1) {127 let getInstances;128 const { selection } = figma.currentPage;129 const selectionFill = selection[0].fills[0];130 const { type } = selectionFill;131 getInstances = figma.currentPage132 .findAll((node) => node.fills[0])133 .filter((node) => node.fills[0].type === type);134 figma.currentPage.selection = getInstances;135 figma.notify(`${getInstances.length} ${type} fill nodes selected`);136 }137 }138 if (msg.type === "get-instances-by-fillVisibility") {139 if (figma.currentPage.selection.length === 1) {140 let getInstances;141 const { selection } = figma.currentPage;142 const selectionFill = selection[0].fills[0];143 const { visible } = selectionFill;144 getInstances = figma.currentPage145 .findAll((node) => node.fills[0])146 .filter((node) => node.fills[0].visible === visible);147 figma.currentPage.selection = getInstances;148 figma.notify(`${getInstances.length} ${visible === true ? "visible" : "hidden"} fill nodes selected`);149 }150 }151 if (msg.type === "get-instances-by-fillOpacity") {152 if (figma.currentPage.selection.length === 1) {153 let getInstances;154 const { selection } = figma.currentPage;155 const selectionFill = selection[0].fills[0];156 const { opacity } = selectionFill;157 getInstances = figma.currentPage158 .findAll((node) => node.fills[0])159 .filter((node) => node.fills[0].opacity === opacity);160 figma.currentPage.selection = getInstances;161 figma.notify(`${getInstances.length} ${Math.round(opacity * 100)}% fill nodes selected`);162 }163 }164 if (msg.type === "get-instances-by-fillBlendMode") {165 if (figma.currentPage.selection.length === 1) {166 let getInstances;167 const { selection } = figma.currentPage;168 const selectionFill = selection[0].fills[0];169 const { blendMode } = selectionFill;170 getInstances = figma.currentPage171 .findAll((node) => node.fills[0])172 .filter((node) => node.fills[0].blendMode === blendMode);173 figma.currentPage.selection = getInstances;174 figma.notify(`${getInstances.length} ${blendMode.replaceAll("_", " ")} fill nodes selected`);175 }176 }177 if (msg.type === "get-instances-by-strokes") {178 if (figma.currentPage.selection.length === 1) {179 let getInstances;180 const { selection } = figma.currentPage;181 const selectionStrokes = selection[0].strokes;182 getInstances = figma.currentPage183 .findAll((node) => node.strokes[0] && node.strokes[0].color !== undefined)184 .filter((node) => node.strokes[0].color.r === selectionStrokes[0].color.r &&185 node.strokes[0].color.g === selectionStrokes[0].color.g &&186 node.strokes[0].color.b === selectionStrokes[0].color.b);187 figma.currentPage.selection = getInstances;188 figma.notify(`${getInstances.length} nodes selected`);189 }190 }191 if (msg.type === "get-instances-by-strokeType") {192 if (figma.currentPage.selection.length === 1) {193 let getInstances;194 const { selection } = figma.currentPage;195 const selectionStroke = selection[0].strokes[0];196 const { type } = selectionStroke;197 getInstances = figma.currentPage198 .findAll((node) => node.strokes[0])199 .filter((node) => node.strokes[0].type === type);200 figma.currentPage.selection = getInstances;201 figma.notify(`${getInstances.length} ${type} stroke nodes selected`);202 }203 }204 if (msg.type === "get-instances-by-strokeVisibility") {205 if (figma.currentPage.selection.length === 1) {206 let getInstances;207 const { selection } = figma.currentPage;208 const selectionStroke = selection[0].strokes[0];209 const { visible } = selectionStroke;210 getInstances = figma.currentPage211 .findAll((node) => node.strokes[0])212 .filter((node) => node.strokes[0].visible === visible);213 figma.currentPage.selection = getInstances;214 figma.notify(`${getInstances.length} ${visible === true ? "visible" : "hidden"} stroke nodes selected`);215 }216 }217 if (msg.type === "get-instances-by-strokeOpacity") {218 if (figma.currentPage.selection.length === 1) {219 let getInstances;220 const { selection } = figma.currentPage;221 const selectionStroke = selection[0].strokes[0];222 const { opacity } = selectionStroke;223 getInstances = figma.currentPage224 .findAll((node) => node.strokes[0])225 .filter((node) => node.strokes[0].opacity === opacity);226 figma.currentPage.selection = getInstances;227 figma.notify(`${getInstances.length} ${Math.round(opacity * 100)}% stroke nodes selected`);228 }229 }230 if (msg.type === "get-instances-by-strokeBlendMode") {231 if (figma.currentPage.selection.length === 1) {232 let getInstances;233 const { selection } = figma.currentPage;234 const selectionStroke = selection[0].strokes[0];235 const { blendMode } = selectionStroke;236 getInstances = figma.currentPage237 .findAll((node) => node.strokes[0])238 .filter((node) => node.strokes[0].blendMode === blendMode);239 figma.currentPage.selection = getInstances;240 figma.notify(`${getInstances.length} ${blendMode.replaceAll("_", " ")} stroke nodes selected`);241 }242 }243 if (msg.type === "get-instances-by-strokeWeight") {244 let nodeStrokeWeight;245 let getInstances;246 let nodetypes = [247 "FRAME",248 "COMPONENT",249 "INSTANCE",250 "GROUP",251 "VECTOR",252 "LINE",253 "STAR",254 "ELLIPSE",255 "RECTANGLE",256 "POLYGON",257 "SHAPE_WITH_TEXT",258 "STICKY",259 "CONNECTOR",260 "STAMP",261 ];262 const { selection } = figma.currentPage;263 function getInstanceByStrokeWeight(node) {264 nodetypes.includes(node.type)265 ? (nodeStrokeWeight = node.strokeWeight)266 : null;267 }268 getInstanceByStrokeWeight(selection[0]);269 getInstances = figma.currentPage270 .findAll()271 .filter((node) => node.type === "TEXT" && node.strokeWeight === nodeStrokeWeight);272 figma.currentPage.selection = getInstances;273 figma.notify(`${getInstances.length} nodes selected`);274 }275 if (msg.type === "get-instances-by-textSize") {276 let nodeTextSize;277 let getInstances;278 const { selection } = figma.currentPage;279 function getInstanceByTextSize(node) {280 node.type === "TEXT" ? (nodeTextSize = node.fontSize) : null;281 }282 getInstanceByTextSize(selection[0]);283 getInstances = figma.currentPage284 .findAll()285 .filter((node) => node.type === "TEXT" && node.fontSize === nodeTextSize);286 figma.currentPage.selection = getInstances;287 figma.notify(`${getInstances.length} nodes selected`);288 }289 if (msg.type === "get-instances-by-fontName") {290 figma.currentPage.selection.length === 0 &&291 figma.notify("You need to select some text to continue");292 if (figma.currentPage.selection.length === 1) {293 let getInstances = [];294 let selectionFont = figma.currentPage.selection[0].fontName;295 getInstances = figma.currentPage296 .findAll((node) => node.type === "TEXT")297 .filter((node) => node.fontName !== figma.mixed && selectionFont !== figma.mixed298 ? node.fontName.family === selectionFont.family &&299 node.fontName.style === selectionFont.style300 : null);301 figma.currentPage.selection = getInstances;302 figma.notify(`${getInstances.length} ${selectionFont !== figma.mixed ? selectionFont.family : null} (${selectionFont !== figma.mixed ? selectionFont.style : null}) selected`);303 }304 }305 if (msg.type === "get-instances-by-fontFamily") {306 figma.currentPage.selection.length === 0 &&307 figma.notify("You need to select some text to continue");308 if (figma.currentPage.selection.length === 1) {309 let getInstances = [];310 let selectionFont = figma.currentPage.selection[0].fontName;311 getInstances = figma.currentPage312 .findAll((node) => node.type === "TEXT")313 .filter((node) => node.fontName !== figma.mixed && selectionFont !== figma.mixed314 ? node.fontName.family === selectionFont.family315 : null);316 figma.currentPage.selection = getInstances;317 figma.notify(`${getInstances.length} ${selectionFont !== figma.mixed ? selectionFont.family : null} selected`);318 }319 }320 if (msg.type === "get-instances-by-fontStyle") {321 figma.currentPage.selection.length === 0 &&322 figma.notify("You need to select some text to continue");323 if (figma.currentPage.selection.length === 1) {324 let getInstances = [];325 let selectionFont = figma.currentPage.selection[0].fontName;326 getInstances = figma.currentPage327 .findAll((node) => node.type === "TEXT")328 .filter((node) => node.fontName !== figma.mixed && selectionFont !== figma.mixed329 ? node.fontName.style === selectionFont.style330 : null);331 figma.currentPage.selection = getInstances;332 figma.notify(`${getInstances.length} ${selectionFont !== figma.mixed ? selectionFont.style : null} selected`);333 }334 }335 if (msg.type === "get-instances-by-characters") {336 const nodes = figma.currentPage.findAll();337 let getInstances;338 getInstances = nodes.filter((node) => node.characters ===339 figma.currentPage.selection[0].characters &&340 node.characters.length ===341 figma.currentPage.selection[0].characters.length);342 figma.currentPage.selection = getInstances;343 figma.notify(`${getInstances.length} nodes selected`);344 }345 if (msg.type === "get-instances-by-findBySelect") {346 let getInstances = [];347 getInstances = figma.currentPage348 .findAll((node) => node.type === "TEXT")349 .filter((node) => node.fontName !== figma.mixed && msg.pickFromSelect !== figma.mixed350 ? node.fontName.family === msg.pickFromSelect351 : null);352 figma.currentPage.selection = getInstances;353 figma.notify(`${getInstances.length} ${msg.pickFromSelect !== figma.mixed ? msg.pickFromSelect : null} selected`);354 }355 // if (msg.type === "get-instances-by-effects") {356 // let nodeEffects = [{}];357 // let getInstances;358 // let nodetypes = [359 // "FRAME",360 // "COMPONENT",361 // "INSTANCE",362 // "GROUP",363 // "VECTOR",364 // "LINE",365 // "STAR",366 // "ELLIPSE",367 // "RECTANGLE",368 // "POLYGON",369 // "TEXT",370 // ];371 // const { selection } = figma.currentPage;372 // async function getInstanceByEffects(node) {373 // nodeEffects = node.effects.map((e) =>374 // nodetypes.includes(node.type)375 // ? {376 // type: e.type,377 // color: e.color,378 // blendMode: e.blendMode,379 // offset: e.offset,380 // spread: e.spread,381 // }382 // : e383 // );384 // console.log(nodeEffects);385 // }386 // getInstanceByEffects(selection[0]);387 // getInstances = figma.currentPage388 // .findAll()389 // .filter(390 // (node) => nodetypes.includes(node.type) && node.effects === nodeEffects391 // );392 // console.log(getInstances);393 // figma.currentPage.selection = getInstances;394 // figma.notify(`${getInstances.length} nodes selected`);395 // }...
code.ts
Source:code.ts
1figma.showUI(__html__, { width: 400, height: 610 });2async function findAllFonts() {3 const nodes = [];4 figma.currentPage5 .findAll((node) => node.type === "TEXT")6 .forEach((node: TextNode) =>7 nodes.push(node.fontName !== figma.mixed && node.fontName.family)8 );9 const uniqueFonts = [...new Set(nodes)];10 const selectList = uniqueFonts.filter((val) => val !== false);11 figma.ui.postMessage({ fontNames: selectList });12}13findAllFonts();14figma.ui.onmessage = (msg) => {15 if (msg.type === "get-instances-by-name") {16 let nodeName: String;17 let getInstances;18 const { selection } = figma.currentPage;19 function getInstanceByName() {20 nodeName = selection[0].name;21 }22 getInstanceByName();23 getInstances = figma.currentPage24 .findAll()25 .filter((node) => node.name === nodeName);26 figma.currentPage.selection = getInstances;27 figma.notify(`${getInstances.length} "${nodeName}'s" selected`);28 }29 if (msg.type === "get-instances-by-type") {30 let nodeType: String;31 let getInstances;32 const { selection } = figma.currentPage;33 function getInstanceByType() {34 nodeType = selection[0].type;35 }36 getInstanceByType();37 getInstances = figma.currentPage38 .findAll()39 .filter((node) => node.type === nodeType);40 figma.currentPage.selection = getInstances;41 figma.notify(`${getInstances.length} "${nodeType}'s" selected`);42 }43 if (msg.type === "get-instances-by-size") {44 let nodeWidth: Number;45 let nodeHeight: Number;46 let getInstances;47 const { selection } = figma.currentPage;48 function getInstanceBySize() {49 nodeWidth = selection[0].width;50 nodeHeight = selection[0].height;51 }52 getInstanceBySize();53 getInstances = figma.currentPage54 .findAll()55 .filter((node) => node.width === nodeWidth && node.height === nodeHeight);56 figma.currentPage.selection = getInstances;57 figma.notify(`${getInstances.length} nodes selected`);58 }59 if (msg.type === "get-instances-by-width") {60 let nodeWidth: Number;61 let getInstances;62 const { selection } = figma.currentPage;63 function getInstanceByWidth() {64 nodeWidth = selection[0].width;65 }66 getInstanceByWidth();67 getInstances = figma.currentPage68 .findAll()69 .filter((node) => node.width === nodeWidth);70 figma.currentPage.selection = getInstances;71 figma.notify(`${getInstances.length} nodes selected`);72 }73 if (msg.type === "get-instances-by-height") {74 let nodeHeight: Number;75 let getInstances;76 const { selection } = figma.currentPage;77 function getInstanceByHeight() {78 nodeHeight = selection[0].height;79 }80 getInstanceByHeight();81 getInstances = figma.currentPage82 .findAll()83 .filter((node) => node.height === nodeHeight);84 figma.currentPage.selection = getInstances;85 figma.notify(`${getInstances.length} nodes selected`);86 }87 if (msg.type === "get-instances-by-radius") {88 let nodeRadius: String;89 let getInstances;90 let nodetypes = [91 "FRAME",92 "COMPONENT",93 "INSTANCE",94 "GROUP",95 "VECTOR",96 "LINE",97 "STAR",98 "ELLIPSE",99 "RECTANGLE",100 "POLYGON",101 ];102 const { selection } = figma.currentPage;103 function getInstanceByRadius(node) {104 nodetypes.includes(node.type) ? (nodeRadius = node.cornerRadius) : null;105 }106 getInstanceByRadius(selection[0]);107 getInstances = figma.currentPage108 .findAll()109 .filter(110 (node: any) =>111 nodetypes.includes(node.type) && node.cornerRadius === nodeRadius112 );113 figma.currentPage.selection = getInstances;114 figma.notify(`${getInstances.length} nodes selected`);115 }116 if (msg.type === "get-instances-by-fills") {117 if (figma.currentPage.selection.length === 1) {118 let getInstances;119 const { selection } = figma.currentPage;120 const selectionFills = (selection[0] as any).fills;121 getInstances = figma.currentPage122 .findAll((node: any) => node.fills[0].color !== undefined)123 .filter(124 (node: any) =>125 node.fills[0].color.r === selectionFills[0].color.r &&126 node.fills[0].color.g === selectionFills[0].color.g &&127 node.fills[0].color.b === selectionFills[0].color.b128 );129 figma.currentPage.selection = getInstances;130 figma.notify(`${getInstances.length} fill nodes selected`);131 }132 }133 if (msg.type === "get-instances-by-fillType") {134 if (figma.currentPage.selection.length === 1) {135 let getInstances;136 const { selection } = figma.currentPage;137 const selectionFill = (selection[0] as any).fills[0];138 const { type } = selectionFill;139 getInstances = figma.currentPage140 .findAll((node: any) => node.fills[0])141 .filter((node: any) => node.fills[0].type === type);142 figma.currentPage.selection = getInstances;143 figma.notify(`${getInstances.length} ${type} fill nodes selected`);144 }145 }146 if (msg.type === "get-instances-by-fillVisibility") {147 if (figma.currentPage.selection.length === 1) {148 let getInstances;149 const { selection } = figma.currentPage;150 const selectionFill = (selection[0] as any).fills[0];151 const { visible } = selectionFill;152 getInstances = figma.currentPage153 .findAll((node: any) => node.fills[0])154 .filter((node: any) => node.fills[0].visible === visible);155 figma.currentPage.selection = getInstances;156 figma.notify(157 `${getInstances.length} ${158 visible === true ? "visible" : "hidden"159 } fill nodes selected`160 );161 }162 }163 if (msg.type === "get-instances-by-fillOpacity") {164 if (figma.currentPage.selection.length === 1) {165 let getInstances;166 const { selection } = figma.currentPage;167 const selectionFill = (selection[0] as any).fills[0];168 const { opacity } = selectionFill;169 getInstances = figma.currentPage170 .findAll((node: any) => node.fills[0])171 .filter((node: any) => node.fills[0].opacity === opacity);172 figma.currentPage.selection = getInstances;173 figma.notify(174 `${getInstances.length} ${Math.round(175 opacity * 100176 )}% fill nodes selected`177 );178 }179 }180 if (msg.type === "get-instances-by-fillBlendMode") {181 if (figma.currentPage.selection.length === 1) {182 let getInstances;183 const { selection } = figma.currentPage;184 const selectionFill = (selection[0] as any).fills[0];185 const { blendMode } = selectionFill;186 getInstances = figma.currentPage187 .findAll((node: any) => node.fills[0])188 .filter((node: any) => node.fills[0].blendMode === blendMode);189 figma.currentPage.selection = getInstances;190 figma.notify(191 `${getInstances.length} ${blendMode.replaceAll(192 "_",193 " "194 )} fill nodes selected`195 );196 }197 }198 if (msg.type === "get-instances-by-strokes") {199 if (figma.currentPage.selection.length === 1) {200 let getInstances;201 const { selection } = figma.currentPage;202 const selectionStrokes = (selection[0] as any).strokes;203 getInstances = figma.currentPage204 .findAll(205 (node: any) => node.strokes[0] && node.strokes[0].color !== undefined206 )207 .filter(208 (node: any) =>209 node.strokes[0].color.r === selectionStrokes[0].color.r &&210 node.strokes[0].color.g === selectionStrokes[0].color.g &&211 node.strokes[0].color.b === selectionStrokes[0].color.b212 );213 figma.currentPage.selection = getInstances;214 figma.notify(`${getInstances.length} nodes selected`);215 }216 }217 if (msg.type === "get-instances-by-strokeType") {218 if (figma.currentPage.selection.length === 1) {219 let getInstances;220 const { selection } = figma.currentPage;221 const selectionStroke = (selection[0] as any).strokes[0];222 const { type } = selectionStroke;223 getInstances = figma.currentPage224 .findAll((node: any) => node.strokes[0])225 .filter((node: any) => node.strokes[0].type === type);226 figma.currentPage.selection = getInstances;227 figma.notify(`${getInstances.length} ${type} stroke nodes selected`);228 }229 }230 if (msg.type === "get-instances-by-strokeVisibility") {231 if (figma.currentPage.selection.length === 1) {232 let getInstances;233 const { selection } = figma.currentPage;234 const selectionStroke = (selection[0] as any).strokes[0];235 const { visible } = selectionStroke;236 getInstances = figma.currentPage237 .findAll((node: any) => node.strokes[0])238 .filter((node: any) => node.strokes[0].visible === visible);239 figma.currentPage.selection = getInstances;240 figma.notify(241 `${getInstances.length} ${242 visible === true ? "visible" : "hidden"243 } stroke nodes selected`244 );245 }246 }247 if (msg.type === "get-instances-by-strokeOpacity") {248 if (figma.currentPage.selection.length === 1) {249 let getInstances;250 const { selection } = figma.currentPage;251 const selectionStroke = (selection[0] as any).strokes[0];252 const { opacity } = selectionStroke;253 getInstances = figma.currentPage254 .findAll((node: any) => node.strokes[0])255 .filter((node: any) => node.strokes[0].opacity === opacity);256 figma.currentPage.selection = getInstances;257 figma.notify(258 `${getInstances.length} ${Math.round(259 opacity * 100260 )}% stroke nodes selected`261 );262 }263 }264 if (msg.type === "get-instances-by-strokeBlendMode") {265 if (figma.currentPage.selection.length === 1) {266 let getInstances;267 const { selection } = figma.currentPage;268 const selectionStroke = (selection[0] as any).strokes[0];269 const { blendMode } = selectionStroke;270 getInstances = figma.currentPage271 .findAll((node: any) => node.strokes[0])272 .filter((node: any) => node.strokes[0].blendMode === blendMode);273 figma.currentPage.selection = getInstances;274 figma.notify(275 `${getInstances.length} ${blendMode.replaceAll(276 "_",277 " "278 )} stroke nodes selected`279 );280 }281 }282 if (msg.type === "get-instances-by-strokeWeight") {283 let nodeStrokeWeight;284 let getInstances;285 let nodetypes = [286 "FRAME",287 "COMPONENT",288 "INSTANCE",289 "GROUP",290 "VECTOR",291 "LINE",292 "STAR",293 "ELLIPSE",294 "RECTANGLE",295 "POLYGON",296 "SHAPE_WITH_TEXT",297 "STICKY",298 "CONNECTOR",299 "STAMP",300 ];301 const { selection } = figma.currentPage;302 function getInstanceByStrokeWeight(node) {303 nodetypes.includes(node.type)304 ? (nodeStrokeWeight = node.strokeWeight)305 : null;306 }307 getInstanceByStrokeWeight(selection[0]);308 getInstances = figma.currentPage309 .findAll()310 .filter(311 (node) => node.type === "TEXT" && node.strokeWeight === nodeStrokeWeight312 );313 figma.currentPage.selection = getInstances;314 figma.notify(`${getInstances.length} nodes selected`);315 }316 if (msg.type === "get-instances-by-textSize") {317 let nodeTextSize;318 let getInstances;319 const { selection } = figma.currentPage;320 function getInstanceByTextSize(node) {321 node.type === "TEXT" ? (nodeTextSize = node.fontSize) : null;322 }323 getInstanceByTextSize(selection[0]);324 getInstances = figma.currentPage325 .findAll()326 .filter((node) => node.type === "TEXT" && node.fontSize === nodeTextSize);327 figma.currentPage.selection = getInstances;328 figma.notify(`${getInstances.length} nodes selected`);329 }330 if (msg.type === "get-instances-by-fontName") {331 figma.currentPage.selection.length === 0 &&332 figma.notify("You need to select some text to continue");333 if (figma.currentPage.selection.length === 1) {334 let getInstances: SceneNode[] = [];335 let selectionFont = (figma.currentPage.selection[0] as TextNode).fontName;336 getInstances = figma.currentPage337 .findAll((node) => node.type === "TEXT")338 .filter((node: TextNode) =>339 node.fontName !== figma.mixed && selectionFont !== figma.mixed340 ? node.fontName.family === selectionFont.family &&341 node.fontName.style === selectionFont.style342 : null343 );344 figma.currentPage.selection = getInstances;345 figma.notify(346 `${getInstances.length} ${347 selectionFont !== figma.mixed ? selectionFont.family : null348 } (${349 selectionFont !== figma.mixed ? selectionFont.style : null350 }) selected`351 );352 }353 }354 if (msg.type === "get-instances-by-fontFamily") {355 figma.currentPage.selection.length === 0 &&356 figma.notify("You need to select some text to continue");357 if (figma.currentPage.selection.length === 1) {358 let getInstances: SceneNode[] = [];359 let selectionFont = (figma.currentPage.selection[0] as TextNode).fontName;360 getInstances = figma.currentPage361 .findAll((node) => node.type === "TEXT")362 .filter((node: TextNode) =>363 node.fontName !== figma.mixed && selectionFont !== figma.mixed364 ? node.fontName.family === selectionFont.family365 : null366 );367 figma.currentPage.selection = getInstances;368 figma.notify(369 `${getInstances.length} ${370 selectionFont !== figma.mixed ? selectionFont.family : null371 } selected`372 );373 }374 }375 if (msg.type === "get-instances-by-fontStyle") {376 figma.currentPage.selection.length === 0 &&377 figma.notify("You need to select some text to continue");378 if (figma.currentPage.selection.length === 1) {379 let getInstances: SceneNode[] = [];380 let selectionFont = (figma.currentPage.selection[0] as TextNode).fontName;381 getInstances = figma.currentPage382 .findAll((node) => node.type === "TEXT")383 .filter((node: TextNode) =>384 node.fontName !== figma.mixed && selectionFont !== figma.mixed385 ? node.fontName.style === selectionFont.style386 : null387 );388 figma.currentPage.selection = getInstances;389 figma.notify(390 `${getInstances.length} ${391 selectionFont !== figma.mixed ? selectionFont.style : null392 } selected`393 );394 }395 }396 if (msg.type === "get-instances-by-characters") {397 const nodes = figma.currentPage.findAll();398 let getInstances;399 getInstances = nodes.filter(400 (node: TextNode) =>401 node.characters ===402 (figma.currentPage.selection[0] as TextNode).characters &&403 node.characters.length ===404 (figma.currentPage.selection[0] as TextNode).characters.length405 );406 figma.currentPage.selection = getInstances;407 figma.notify(`${getInstances.length} nodes selected`);408 }409 if (msg.type === "get-instances-by-findBySelect") {410 let getInstances: SceneNode[] = [];411 getInstances = figma.currentPage412 .findAll((node) => node.type === "TEXT")413 .filter((node: TextNode) =>414 node.fontName !== figma.mixed && msg.pickFromSelect !== figma.mixed415 ? node.fontName.family === msg.pickFromSelect416 : null417 );418 figma.currentPage.selection = getInstances;419 figma.notify(420 `${getInstances.length} ${421 msg.pickFromSelect !== figma.mixed ? msg.pickFromSelect : null422 } selected`423 );424 }425 // if (msg.type === "get-instances-by-effects") {426 // let nodeEffects = [{}];427 // let getInstances;428 // let nodetypes = [429 // "FRAME",430 // "COMPONENT",431 // "INSTANCE",432 // "GROUP",433 // "VECTOR",434 // "LINE",435 // "STAR",436 // "ELLIPSE",437 // "RECTANGLE",438 // "POLYGON",439 // "TEXT",440 // ];441 // const { selection } = figma.currentPage;442 // async function getInstanceByEffects(node) {443 // nodeEffects = node.effects.map((e) =>444 // nodetypes.includes(node.type)445 // ? {446 // type: e.type,447 // color: e.color,448 // blendMode: e.blendMode,449 // offset: e.offset,450 // spread: e.spread,451 // }452 // : e453 // );454 // console.log(nodeEffects);455 // }456 // getInstanceByEffects(selection[0]);457 // getInstances = figma.currentPage458 // .findAll()459 // .filter(460 // (node) => nodetypes.includes(node.type) && node.effects === nodeEffects461 // );462 // console.log(getInstances);463 // figma.currentPage.selection = getInstances;464 // figma.notify(`${getInstances.length} nodes selected`);465 // }...
Using AI Code Generation
1var root = new Root();2var instances = root.getInstances();3for (var i = 0; i < instances.length; i++) {4 console.log(instances[i].getLabel());5}6var root = new Root();7var instances = root.getInstances();8for (var i = 0; i < instances.length; i++) {9 console.log(instances[i].getLabel());10}11var root = new Root();12var instances = root.getInstances();13for (var i = 0; i < instances.length; i++) {14 console.log(instances[i].getLabel());15}16var root = new Root();17var instances = root.getInstances();18for (var i = 0; i < instances.length; i++) {19 console.log(instances[i].getLabel());20}21var root = new Root();22var instances = root.getInstances();23for (var i = 0; i < instances.length; i++) {24 console.log(instances[i].getLabel());25}26var root = new Root();27var instances = root.getInstances();28for (var i = 0; i < instances.length; i++) {29 console.log(instances[i].getLabel());30}31var root = new Root();32var instances = root.getInstances();33for (var i = 0; i < instances.length; i++) {34 console.log(instances[i].getLabel());35}36var root = new Root();37var instances = root.getInstances();38for (var i = 0; i < instances.length; i++) {39 console.log(instances[i].getLabel());40}41var root = new Root();42var instances = root.getInstances();43for (var i = 0; i < instances.length; i++) {44 console.log(instances[i].getLabel());45}
Using AI Code Generation
1var folder = FolderManager.getFolder("root");2var instances = folder.getInstances();3for(var i = 0; i < instances.length; i++){4 var instance = instances[i];5}6var folder = FolderManager.getFolder("root/test");7var instances = folder.getInstances();8for(var i = 0; i < instances.length; i++){9 var instance = instances[i];10}11var folder = FolderManager.getFolder("root/test");12var instances = folder.getInstances(0, 10);13for(var i = 0; i < instances.length; i++){14 var instance = instances[i];15}16var folder = FolderManager.getFolder("root/test");17var instances = folder.getInstances(0, 10, "asc");18for(var i = 0; i < instances.length; i++){19 var instance = instances[i];20}21var folder = FolderManager.getFolder("root/test");22var instances = folder.getInstances(0, 10, "asc", "name");23for(var i = 0; i < instances.length; i++){24 var instance = instances[i];25}26var folder = FolderManager.getFolder("root/test");27var instances = folder.getInstances(0, 10, "asc", "name", "instance");28for(var i = 0; i < instances.length; i++){29 var instance = instances[i];30}31var folder = FolderManager.getFolder("root/test");32var instances = folder.getInstances(0, 10, "asc", "name", "instance", "process");33for(var i = 0; i < instances.length; i++){34 var instance = instances[i];35}
Using AI Code Generation
1var root = new Root();2var instances = root.getInstances();3var instance = instances[0];4var parent = instance.parent;5var instances = parent.getInstances();6var instance = instances[0];7var child = instance.children[0];8var instances = child.getInstances();9var instance = instances[0];10var component = instance.component;11var instances = component.getInstances();12var instance = instances[0];13var layer = instance.layer;14var instances = layer.getInstances();15var instance = instances[0];16var page = instance.page;17var instances = page.getInstances();18var instance = instances[0];19var document = instance.document;20var instances = document.getInstances();21var instance = instances[0];22var artboard = instance.artboard;23var instances = artboard.getInstances();24var instance = instances[0];25var group = instance.group;26var instances = group.getInstances();27var instance = instances[0];28var slice = instance.slice;29var instances = slice.getInstances();30var instance = instances[0];31var component = instance.component;32var instances = component.getInstances();33var instance = instances[0];34var symbol = instance.symbol;35var instances = symbol.getInstances();36var instance = instances[0];37var symbolMaster = instance.symbolMaster;38var instances = symbolMaster.getInstances();39var instance = instances[0];40var shape = instance.shape;41var instances = shape.getInstances();42var instance = instances[0];43var rect = instance.rect;44var instances = rect.getInstances();45var instance = instances[0];46var oval = instance.oval;47var instances = oval.getInstances();48var instance = instances[0];49var star = instance.star;
Using AI Code Generation
1var rootNS = require('root');2var instances = rootNS.getInstances();3for (var i = 0; i < instances.length; i++) {4 console.log(instances[i].name);5}6var ns1 = require('ns1');7var instances = ns1.getInstances();8for (var i = 0; i < instances.length; i++) {9 console.log(instances[i].name);10}11var ns2 = require('ns1/ns2');12var instances = ns2.getInstances();13for (var i = 0; i < instances.length; i++) {14 console.log(instances[i].name);15}16var ns3 = require('ns1/ns2/ns3');17var instances = ns3.getInstances();18for (var i = 0; i < instances.length; i++) {19 console.log(instances[i].name);20}21var ns4 = require('ns1/ns2/ns3/ns4');22var instances = ns4.getInstances();23for (var i = 0; i < instances.length; i++) {24 console.log(instances[i].name);25}26var ns5 = require('ns1/ns2/ns3/ns4/ns5');27var instances = ns5.getInstances();28for (var i = 0; i < instances.length; i++) {29 console.log(instances[i
Using AI Code Generation
1var instances = root.getInstances();2var instance = instances[0];3var instanceId = instance.getInstanceId();4var instance = root.getInstance(instanceId);5var instanceId = instance.getInstanceId();6var instances = instance.getInstances();7var instance = instances[0];8var instanceId = instance.getInstanceId();9var instance = instance.getInstance(instanceId);10var instanceId = instance.getInstanceId();11var instances = instance.getInstances();12var instance = instances[0];13var instanceId = instance.getInstanceId();14var instance = instance.getInstance(instanceId);15var instanceId = instance.getInstanceId();16var instances = instance.getInstances();17var instance = instances[0];18var instanceId = instance.getInstanceId();19var instance = instance.getInstance(instanceId);20var instanceId = instance.getInstanceId();21var instances = instance.getInstances();22var instance = instances[0];23var instanceId = instance.getInstanceId();24var instance = instance.getInstance(instanceId);25var instanceId = instance.getInstanceId();26var instances = instance.getInstances();27var instance = instances[0];28var instanceId = instance.getInstanceId();29var instance = instance.getInstance(instanceId);30var instanceId = instance.getInstanceId();31var instances = instance.getInstances();32var instance = instances[0];33var instanceId = instance.getInstanceId();34var instance = instance.getInstance(instanceId);35var instanceId = instance.getInstanceId();36var instances = instance.getInstances();37var instance = instances[0];38var instanceId = instance.getInstanceId();39var instance = instance.getInstance(instanceId);40var instanceId = instance.getInstanceId();41var instances = instance.getInstances();42var instance = instances[0];43var instanceId = instance.getInstanceId();44var instance = instance.getInstance(instanceId);
Using AI Code Generation
1var obj = new root();2var instances = obj.getInstances();3var root = function() {4 this.getInstances = function() {5 return instances;6 }7}8var instance = function() {9 var instances = [];10 instances.push(this);11}12var obj = new root();13var instances = obj.getInstances();14var root = function() {15 this.getInstances = function() {16 return instances;17 }18}19var instance = function() {20 var instances = [];21 instances.push(this);22}23var obj = new root();24var instances = obj.getInstances();25var root = function() {26 this.getInstances = function() {27 return instances;28 }29}30var instance = function() {31 var instances = [];32 instances.push(this);33}
Using AI Code Generation
1var root = application.getRoot();2var instances = root.getInstances("myInstance");3var instance = instances[0];4var component = root.find("myComponent");5instances = component.getInstances("myInstance");6instance = instances[0];7var component = root.find("myComponent");8instances = component.getInstances("myInstance");9instance = instances[0];10var component = root.find("myComponent");11instances = component.getInstances("myInstance");12instance = instances[0];13var root = application.getRoot();14var instances = root.getInstances("myInstance");15var instance = instances[0];16var component = root.find("myComponent");17instances = component.getInstances("myInstance");18instance = instances[0];19var component = root.find("myComponent");20instances = component.getInstances("myInstance");21instance = instances[0];22var component = root.find("myComponent");23instances = component.getInstances("myInstance");24instance = instances[0];25var root = application.getRoot();26var instances = root.getInstances("myInstance");27var instance = instances[0];28var component = root.find("myComponent");29instances = component.getInstances("myInstance");30instance = instances[0];31var component = root.find("myComponent");32instances = component.getInstances("myInstance");33instance = instances[0];34var component = root.find("myComponent");35instances = component.getInstances("myInstance");36instance = instances[0];37var root = application.getRoot();38var instances = root.getInstances("myInstance");39var instance = instances[0];40var component = root.find("myComponent");41instances = component.getInstances("my
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!