CarrollCloud / js-value-wrappers (public) (License: GPLv3) (since 2018-06-24) (hash sha1)
Wrappers are classes that contain a specific value, and have methods to work on said value.
Example: Dimension to store length values, contains methods to convert and display the dimension in various ways.
List of commits:
Subject Hash Author Date (UTC)
DimWrapper is to hold a dimension. 46e9f9f9123b270f2ce4d3cc84d73f55c4cdf8ba Carroll 2018-06-24 02:11:47
NumericWrapper is to hold a number. Go figure. 2daf55f2e94126859ed26bdc9cb9e4e3f33541eb Carroll 2018-06-24 02:05:19
Wrapper is the base class for this package. 552caf51d2c7e4400dafe788e632972cd860b97e Carroll 2018-06-24 02:03:47
Uploading metadata 6ec5ca0bf2000ad865e4f64ddc26e2e64a0d6a99 Carroll 2018-06-24 02:02:09
Commit 46e9f9f9123b270f2ce4d3cc84d73f55c4cdf8ba - DimWrapper is to hold a dimension.
One can create a new object with feet, inches, or meters. If there is a
need, we may add miles, furlongs, cubits, etc.
Author: Carroll
Author date (UTC): 2018-06-24 02:11
Committer name: Carroll
Committer date (UTC): 2018-06-24 02:11
Parent(s): 2daf55f2e94126859ed26bdc9cb9e4e3f33541eb
Signing key:
Tree: 2d4ce065bbe9a070c3276b4137d8493a08dd8cd6
File Lines added Lines deleted
lib/wrappers/DimWrapper.d.ts 53 0
lib/wrappers/DimWrapper.js 116 0
src/wrappers/DimWrapper.ts 118 0
File lib/wrappers/DimWrapper.d.ts added (mode: 100644) (index 0000000..daab91a)
1 import { NumericWrapper } from "./NumericWrapper";
2 /**
3 * Stores a dimension value and provides various methods for operating on it.
4 * The value is stored in meters to keep the system consistent.
5 * There are various outputs for this dimension.
6 */
7 export declare class DimWrapper extends NumericWrapper {
8 /** Number of inches in a meter */
9 static inchInMeter: number;
10 /** Number of feet in a meter */
11 static footInMeter: number;
12 /** The value that is stored in meters */
13 value: number;
14 /** Creates a new instance of wrapper with the given value */
15 constructor(value: number, unit: "inch" | "foot" | "meter");
16 /**
17 * Wraps a dimension **IN METERS** and creates a new DimWrapper
18 * This is primarily used for serialization and called on the prototype chain.
19 */
20 wrap(meter: number): DimWrapper;
21 /** Unwrap the meter stored in this Wrapper */
22 unwrap(): number;
23 /**
24 * Converts the value to the specified unit
25 * @param value Input value to convert
26 * @param fromUnit Unit of input value
27 * @param toUnit Unit to return
28 * @return The specified number in the specified unit
29 */
30 static convert(value: number, fromUnit: "inch" | "foot" | "meter", toUnit: "inch" | "foot" | "meter"): number;
31 /** Returns the stored value in inches */
32 toInches(): number;
33 /** Returns the stored value in feet */
34 toFeet(): number;
35 /** Returns the stored value in meters */
36 toMeters(): number;
37 /** Returns the stored value in centimeters */
38 toCentimeters(): number;
39 /**
40 * Adds the value to the stored dimension
41 * @param value Value to add
42 * @param unit Unit of this value
43 */
44 addValue(value: number, unit: "inch" | "foot" | "meter"): void;
45 /**
46 * Returns this value as a foot inch fraction string
47 * @param denom=16 Denominator for fraction (defaults to 16).
48 * Values that are not whole or greater than 1 default to 16
49 * @param simplifyFraction=true Whether or not to simplify the fraction (defaults to true)
50 * @return `${foot}'-${inch} ${fract}/${denom}`
51 */
52 toFootInchFract(denom?: number, simplifyFraction?: boolean): string;
53 }
File lib/wrappers/DimWrapper.js added (mode: 100644) (index 0000000..41fc976)
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 const NumericWrapper_1 = require("./NumericWrapper");
4 /**
5 * Stores a dimension value and provides various methods for operating on it.
6 * The value is stored in meters to keep the system consistent.
7 * There are various outputs for this dimension.
8 */
9 class DimWrapper extends NumericWrapper_1.NumericWrapper {
10 /** Creates a new instance of wrapper with the given value */
11 constructor(value, unit) {
12 //Convert the value to meters
13 super(DimWrapper.convert(value, unit, "meter"));
14 /** The value that is stored in meters */
15 this.value = 0;
16 }
17 /**
18 * Wraps a dimension **IN METERS** and creates a new DimWrapper
19 * This is primarily used for serialization and called on the prototype chain.
20 */
21 wrap(meter) {
22 return new DimWrapper(meter, "meter");
23 }
24 /** Unwrap the meter stored in this Wrapper */
25 unwrap() {
26 return this.value;
27 }
28 /**
29 * Converts the value to the specified unit
30 * @param value Input value to convert
31 * @param fromUnit Unit of input value
32 * @param toUnit Unit to return
33 * @return The specified number in the specified unit
34 */
35 static convert(value, fromUnit, toUnit) {
36 /** The value in meters */
37 let inMeter = value;
38 //convert the value to meters
39 switch (fromUnit) {
40 case ("inch"):
41 inMeter = value * this.inchInMeter;
42 break;
43 case ("foot"):
44 inMeter = value * this.footInMeter;
45 break;
46 }
47 //Now, we convert the output value
48 switch (toUnit) {
49 case ("inch"):
50 return inMeter / this.inchInMeter;
51 case ("foot"):
52 return inMeter / this.footInMeter;
53 case ("meter"):
54 return inMeter;
55 }
56 }
57 /** Returns the stored value in inches */
58 toInches() {
59 return this.value / DimWrapper.inchInMeter;
60 }
61 /** Returns the stored value in feet */
62 toFeet() {
63 return this.value / DimWrapper.footInMeter;
64 }
65 /** Returns the stored value in meters */
66 toMeters() {
67 return this.value;
68 }
69 /** Returns the stored value in centimeters */
70 toCentimeters() {
71 return this.value * 100;
72 }
73 /**
74 * Adds the value to the stored dimension
75 * @param value Value to add
76 * @param unit Unit of this value
77 */
78 addValue(value, unit) {
79 this.value += DimWrapper.convert(value, unit, "meter");
80 }
81 /**
82 * Returns this value as a foot inch fraction string
83 * @param denom=16 Denominator for fraction (defaults to 16).
84 * Values that are not whole or greater than 1 default to 16
85 * @param simplifyFraction=true Whether or not to simplify the fraction (defaults to true)
86 * @return `${foot}'-${inch} ${fract}/${denom}`
87 */
88 toFootInchFract(denom = 16, simplifyFraction = true) {
89 //The fraction denominator must be greater than 1 and a whole number
90 denom = (denom > 1 && (denom % 2 == 0 || denom % 2 == 1)) ? denom : 16;
91 /** Inch value in decimals */
92 const inchDecimal = this.toInches();
93 /** Foot value without inches */
94 const foot = Math.floor(inchDecimal / 12);
95 /** Inch value without feet or fraction */
96 const inch = Math.floor(inchDecimal % 12);
97 /** Fraction value without feet or inches */
98 let fract = Math.round((inchDecimal - inch) * denom);
99 //Now, we simplify fract and denom (if possible)
100 //Standard in the english system, we only do this by 2
101 if (simplifyFraction)
102 while (denom > 2 && denom % 2 == 0 && fract % 2 == 0) {
103 denom /= 2;
104 fract /= 2;
105 }
106 /** We do not include the fraction string if the value is 0 (and simplify fractions is true)*/
107 const fractString = (simplifyFraction && fract == 0) ? "" : ` ${fract}/${denom}`;
108 //Finally, construct the value and return
109 return `${foot}'-${inch}${fractString}"`;
110 }
111 }
112 /** Number of inches in a meter */
113 DimWrapper.inchInMeter = 39.37008;
114 /** Number of feet in a meter */
115 DimWrapper.footInMeter = 3.28084;
116 exports.DimWrapper = DimWrapper;
File src/wrappers/DimWrapper.ts added (mode: 100644) (index 0000000..a41d09e)
1 import { NumericWrapper } from "./NumericWrapper"
2
3 /**
4 * Stores a dimension value and provides various methods for operating on it.
5 * The value is stored in meters to keep the system consistent.
6 * There are various outputs for this dimension.
7 */
8 export class DimWrapper extends NumericWrapper {
9 /** Number of inches in a meter */
10 static inchInMeter = 39.37008
11 /** Number of feet in a meter */
12 static footInMeter = 3.28084
13 /** The value that is stored in meters */
14 value: number = 0
15 /** Creates a new instance of wrapper with the given value */
16 constructor(value: number, unit: "inch" | "foot" | "meter") {
17 //Convert the value to meters
18 super(DimWrapper.convert(value, unit, "meter"))
19 }
20 /**
21 * Wraps a dimension **IN METERS** and creates a new DimWrapper
22 * This is primarily used for serialization and called on the prototype chain.
23 */
24 wrap(meter: number): DimWrapper {
25 return new DimWrapper(meter, "meter")
26 }
27 /** Unwrap the meter stored in this Wrapper */
28 unwrap() {
29 return this.value
30 }
31 /**
32 * Converts the value to the specified unit
33 * @param value Input value to convert
34 * @param fromUnit Unit of input value
35 * @param toUnit Unit to return
36 * @return The specified number in the specified unit
37 */
38 static convert(value: number,
39 fromUnit: "inch" | "foot" | "meter",
40 toUnit: "inch" | "foot" | "meter"): number {
41 /** The value in meters */
42 let inMeter: number = value
43 //convert the value to meters
44 switch (fromUnit) {
45 case ("inch"):
46 inMeter = value * this.inchInMeter
47 break
48 case ("foot"):
49 inMeter = value * this.footInMeter
50 break
51 }
52 //Now, we convert the output value
53 switch (toUnit) {
54 case ("inch"):
55 return inMeter / this.inchInMeter
56 case ("foot"):
57 return inMeter / this.footInMeter
58 case ("meter"):
59 return inMeter
60 }
61
62 }
63 /** Returns the stored value in inches */
64 toInches() {
65 return this.value / DimWrapper.inchInMeter
66 }
67 /** Returns the stored value in feet */
68 toFeet() {
69 return this.value / DimWrapper.footInMeter
70 }
71 /** Returns the stored value in meters */
72 toMeters() {
73 return this.value
74 }
75 /** Returns the stored value in centimeters */
76 toCentimeters() {
77 return this.value * 100
78 }
79 /**
80 * Adds the value to the stored dimension
81 * @param value Value to add
82 * @param unit Unit of this value
83 */
84 addValue(value: number, unit: "inch" | "foot" | "meter") {
85 this.value += DimWrapper.convert(value, unit, "meter")
86 }
87
88 /**
89 * Returns this value as a foot inch fraction string
90 * @param denom=16 Denominator for fraction (defaults to 16).
91 * Values that are not whole or greater than 1 default to 16
92 * @param simplifyFraction=true Whether or not to simplify the fraction (defaults to true)
93 * @return `${foot}'-${inch} ${fract}/${denom}`
94 */
95 toFootInchFract(denom = 16, simplifyFraction = true): string {
96 //The fraction denominator must be greater than 1 and a whole number
97 denom = (denom > 1 && (denom % 2 == 0 || denom % 2 == 1)) ? denom : 16
98 /** Inch value in decimals */
99 const inchDecimal = this.toInches()
100 /** Foot value without inches */
101 const foot = Math.floor(inchDecimal / 12)
102 /** Inch value without feet or fraction */
103 const inch = Math.floor(inchDecimal % 12)
104 /** Fraction value without feet or inches */
105 let fract = Math.round((inchDecimal - inch) * denom)
106 //Now, we simplify fract and denom (if possible)
107 //Standard in the english system, we only do this by 2
108 if (simplifyFraction)
109 while (denom > 2 && denom % 2 == 0 && fract % 2 == 0) {
110 denom /= 2
111 fract /= 2
112 }
113 /** We do not include the fraction string if the value is 0 (and simplify fractions is true)*/
114 const fractString = (simplifyFraction && fract == 0) ? "" : ` ${fract}/${denom}`
115 //Finally, construct the value and return
116 return `${foot}'-${inch}${fractString}"`
117 }
118 }
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/CarrollCloud/js-value-wrappers

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/CarrollCloud/js-value-wrappers

Clone this repository using git:
git clone git://git.rocketgit.com/user/CarrollCloud/js-value-wrappers

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main