@@ -31,6 +31,7 @@ import * as Glob from 'glob';
3131import * as Marked from 'marked' ;
3232import * as Moment from 'moment' ;
3333import * as Path from 'path' ;
34+ const RandomInt = require ( 'random-int' ) ;
3435import * as sc_contracts from './contracts' ;
3536import * as sc_controller from './controller' ;
3637import * as sc_helpers from './helpers' ;
185186
186187` ;
187188
189+ const MAX_RAND = 2147483647 ;
190+ const MIN_RAND = - 2147483648 ;
191+
188192function _fromMarkdown ( markdown : any ) : string {
189193 markdown = sc_helpers . toStringSafe ( markdown ) ;
190194
@@ -229,9 +233,10 @@ function _generateHelpHTML(): string {
229233 markdown += "| `$noResultInfo(flag?: boolean, permanent?: boolean = false): boolean` | Gets or sets if result should be displayed or not. |\n" ;
230234 markdown += "| `$now(): Moment.Moment` | Returns the current [time](https://momentjs.com/docs/). |\n" ;
231235 markdown += "| `$openHtml(html: string, tabTitle?: string): vscode.Thenable<any>` | Opens a HTML document in a new tab. |\n" ;
236+ markdown += "| `$rand(minOrMax?: number = 0, max?: number = 2147483647): number` | Returns a random integer number. |\n" ;
232237 markdown += "| `$readFile(path: string): Buffer` | Reads the data of a file. |\n" ;
233238 markdown += "| `$readJSON(file: string, encoding?: string = 'utf8'): any` | Reads a JSON file and returns the its object / value. |\n" ;
234- markdown += "| `$readString(file: string, encoding?: string = 'utf8'): any ` | Reads a file as string. |\n" ;
239+ markdown += "| `$readString(file: string, encoding?: string = 'utf8'): string ` | Reads a file as string. |\n" ;
235240 markdown += "| `$require(id: string): any` | Loads a module from execution / extension context. |\n" ;
236241 markdown += "| `$setState(newValue: any): any` | Sets the value of `$state` variable and returns the new value. |\n" ;
237242 markdown += "| `$sha1(data: any, asBuffer: boolean = false): string` | Hashes data by SHA-1. |\n" ;
@@ -636,6 +641,28 @@ export function quickExecution() {
636641 return $me . openHtml ( html , title ) ;
637642 } ;
638643 const $output = $me . outputChannel ;
644+ const $rand = function ( minOrMax ?: number , max ?: number ) : number {
645+ if ( arguments . length < 2 ) {
646+ minOrMax = parseInt ( sc_helpers . toStringSafe ( minOrMax ) . trim ( ) ) ;
647+ if ( isNaN ( minOrMax ) ) {
648+ minOrMax = MAX_RAND ;
649+ }
650+
651+ return RandomInt ( 0 , minOrMax ) ;
652+ }
653+
654+ max = parseInt ( sc_helpers . toStringSafe ( max ) . trim ( ) ) ;
655+ if ( isNaN ( max ) ) {
656+ max = MAX_RAND ;
657+ }
658+
659+ minOrMax = parseInt ( sc_helpers . toStringSafe ( minOrMax ) . trim ( ) ) ;
660+ if ( isNaN ( minOrMax ) ) {
661+ minOrMax = max >= 0 ? 0 : MIN_RAND ;
662+ }
663+
664+ return RandomInt ( minOrMax , max ) ;
665+ } ;
639666 const $readFile = function ( file : string ) : Buffer {
640667 file = sc_helpers . toStringSafe ( file ) ;
641668 if ( ! Path . isAbsolute ( file ) ) {
0 commit comments