Skip to content
This repository was archived by the owner on Jan 10, 2019. It is now read-only.

Commit f2511c3

Browse files
committed
added $rand function for quick executions
1 parent 88439d4 commit f2511c3

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log (vs-script-commands)
22

3+
## 4.9.0 (May 2nd, 2017; quick execution functions)
4+
5+
* added `$rand()` function for [quick executions](https://github.com/mkloubert/vs-script-commands#quick-execution-)
6+
37
## 4.8.0 (May 2nd, 2017; quick execution functions)
48

59
* added `$` and `$readJSON()` and `$readString()` functions for [quick executions](https://github.com/mkloubert/vs-script-commands#quick-execution-)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vs-script-commands",
33
"displayName": "Script Commands",
44
"description": "Adds additional commands to Visual Studio Code that uses scripts (JavaScript) for execution.",
5-
"version": "4.8.0",
5+
"version": "4.9.0",
66
"publisher": "mkloubert",
77
"engines": {
88
"vscode": "^1.6.0"
@@ -457,6 +457,7 @@
457457
"marked": "^0.3.6",
458458
"moment": "^2.17.1",
459459
"node-workflows": "^1.3.1",
460+
"random-int": "^1.0.0",
460461
"tmp": "0.0.31",
461462
"uuid": "^3.0.1"
462463
}

src/quick.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import * as Glob from 'glob';
3131
import * as Marked from 'marked';
3232
import * as Moment from 'moment';
3333
import * as Path from 'path';
34+
const RandomInt = require('random-int');
3435
import * as sc_contracts from './contracts';
3536
import * as sc_controller from './controller';
3637
import * as sc_helpers from './helpers';
@@ -185,6 +186,9 @@ li {
185186
186187
`;
187188

189+
const MAX_RAND = 2147483647;
190+
const MIN_RAND = -2147483648;
191+
188192
function _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

Comments
 (0)