Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into develop
- Loading branch information
Showing
14 changed files
with
249 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const binDocs = { | ||
name: 'bin', | ||
category: 'Utils', | ||
syntax: [ | ||
'bin(value)' | ||
], | ||
description: 'Format a number as binary', | ||
examples: [ | ||
'bin(2)' | ||
], | ||
seealso: ['oct', 'hex'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const hexDocs = { | ||
name: 'hex', | ||
category: 'Utils', | ||
syntax: [ | ||
'hex(value)' | ||
], | ||
description: 'Format a number as hexadecimal', | ||
examples: [ | ||
'hex(240)' | ||
], | ||
seealso: ['bin', 'oct'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const octDocs = { | ||
name: 'oct', | ||
category: 'Utils', | ||
syntax: [ | ||
'oct(value)' | ||
], | ||
description: 'Format a number as octal', | ||
examples: [ | ||
'oct(56)' | ||
], | ||
seealso: ['bin', 'hex'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { factory } from '../../utils/factory' | ||
import { isInteger } from '../../utils/number' | ||
|
||
function baseFormatter (base) { | ||
const prefixes = { 2: '0b', 8: '0o', 16: '0x' } | ||
const prefix = prefixes[base] | ||
return function (n) { | ||
if (n > 2 ** 31 - 1 || n < -(2 ** 31)) { | ||
throw new Error('Value must be in range [-2^31, 2^31-1]') | ||
} | ||
if (!isInteger(n)) { | ||
throw new Error('Value must be an integer') | ||
} | ||
if (n < 0) { | ||
n = n + 2 ** 32 | ||
} | ||
return `${prefix}${n.toString(base)}` | ||
} | ||
} | ||
|
||
const dependencies = ['typed'] | ||
|
||
export function createBaseFormatterFactory (name, base) { | ||
return factory(name, dependencies, ({ typed }) => { | ||
return typed(name, { | ||
number: baseFormatter(base) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createBaseFormatterFactory } from './baseUtils' | ||
|
||
/** | ||
* Format a number as binary. | ||
* | ||
* Syntax: | ||
* | ||
* math.bin(value) | ||
* | ||
* Examples: | ||
* | ||
* //the following outputs "0b10" | ||
* math.bin(2) | ||
* | ||
* See also: | ||
* | ||
* oct | ||
* hex | ||
* | ||
* @param {number} value Value to be stringified | ||
* @return {string} The formatted value | ||
*/ | ||
export const createBin = createBaseFormatterFactory('bin', 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createBaseFormatterFactory } from './baseUtils' | ||
|
||
/** | ||
* Format a number as hexadecimal. | ||
* | ||
* Syntax: | ||
* | ||
* math.hex(value) | ||
* | ||
* Examples: | ||
* | ||
* //the following outputs "0xF0" | ||
* math.hex(240) | ||
* | ||
* See also: | ||
* | ||
* oct | ||
* bin | ||
* | ||
* @param {number} value Value to be stringified | ||
* @return {string} The formatted value | ||
*/ | ||
export const createHex = createBaseFormatterFactory('hex', 16) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { createBaseFormatterFactory } from './baseUtils' | ||
|
||
/** | ||
* Format a number as octal. | ||
* | ||
* Syntax: | ||
* | ||
* math.oct(value) | ||
* | ||
* Examples: | ||
* | ||
* //the following outputs "0o70" | ||
* math.oct(56) | ||
* | ||
* See also: | ||
* | ||
* bin | ||
* hex | ||
* | ||
* @param {number} value Value to be stringified | ||
* @return {string} The formatted value | ||
*/ | ||
|
||
export const createOct = createBaseFormatterFactory('oct', 8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters