Skip to content

Commit 20c443d

Browse files
committed
feat: add array/byte-orders
1 parent 37ef5f4 commit 20c443d

File tree

11 files changed

+502
-0
lines changed

11 files changed

+502
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# byteOrders
22+
23+
> List of byte orders.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var byteOrders = require( '@stdlib/array/byte-orders' );
41+
```
42+
43+
#### byteOrders()
44+
45+
Returns a list of byte orders.
46+
47+
```javascript
48+
var out = byteOrders();
49+
// e.g., returns [ 'little-endian', 'big-endian' ]
50+
```
51+
52+
The output array contains the following orders:
53+
54+
- **little-endian**: bytes are ordered from least-to-most significant byte.
55+
- **big-endian**: bytes are ordered from most-to-least significant byte.
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
62+
63+
<section class="notes">
64+
65+
</section>
66+
67+
<!-- /.notes -->
68+
69+
<!-- Package usage examples. -->
70+
71+
<section class="examples">
72+
73+
## Examples
74+
75+
<!-- eslint no-undef: "error" -->
76+
77+
```javascript
78+
var contains = require( '@stdlib/array/base/assert/contains' ).factory;
79+
var byteOrders = require( '@stdlib/array/byte-orders' );
80+
81+
var isByteOrder = contains( byteOrders() );
82+
83+
var bool = isByteOrder( 'little-endian' );
84+
// returns true
85+
86+
bool = isByteOrder( 'big-endian' );
87+
// returns true
88+
89+
bool = isByteOrder( 'beep' );
90+
// returns false
91+
```
92+
93+
</section>
94+
95+
<!-- /.examples -->
96+
97+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
98+
99+
<section class="references">
100+
101+
</section>
102+
103+
<!-- /.references -->
104+
105+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
106+
107+
<section class="related">
108+
109+
</section>
110+
111+
<!-- /.related -->
112+
113+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
114+
115+
<section class="links">
116+
117+
</section>
118+
119+
<!-- /.links -->
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
25+
var pkg = require( './../package.json' ).name;
26+
var byteOrders = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var out;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
out = byteOrders();
38+
if ( out.length !== 2 ) {
39+
b.fail( 'should return an array of length 2' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isStringArray( out ) ) {
44+
b.fail( 'should return an array of strings' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
{{alias}}()
3+
Returns a list of byte orders.
4+
5+
The output array contains the following orders:
6+
7+
- little-endian: bytes are ordered from least-to-most significant byte.
8+
- big-endian: bytes are ordered from most-to-least significant byte.
9+
10+
Returns
11+
-------
12+
out: Array<string>
13+
List of byte orders.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}()
18+
[...]
19+
20+
See Also
21+
--------
22+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Returns a list of byte orders.
23+
*
24+
* ## Notes
25+
*
26+
* - The output array contains the following orders:
27+
*
28+
* - little-endian: bytes are ordered from least-to-most significant byte.
29+
* - big-endian: bytes are ordered from most-to-least significant byte.
30+
*
31+
* @returns list of byte orders
32+
*
33+
* @example
34+
* var list = byteOrders();
35+
* // e.g., returns [ 'little-endian', 'big-endian' ]
36+
*/
37+
declare function byteOrders(): Array<string>;
38+
39+
40+
// EXPORTS //
41+
42+
export = byteOrders;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import byteOrders = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of strings...
25+
{
26+
byteOrders(); // $ExpectType string[]
27+
}
28+
29+
// The compiler throws an error if the function is provided any arguments...
30+
{
31+
byteOrders( 9 ); // $ExpectError
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var contains = require( '@stdlib/array/base/assert/contains' ).factory;
22+
var byteOrders = require( './../lib' );
23+
24+
var isByteOrder = contains( byteOrders() );
25+
26+
var bool = isByteOrder( 'little-endian' );
27+
console.log( bool );
28+
// => true
29+
30+
bool = isByteOrder( 'big-endian' );
31+
console.log( bool );
32+
// => true
33+
34+
bool = isByteOrder( 'beep' );
35+
console.log( bool );
36+
// => false
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Return a list of byte orders.
23+
*
24+
* @module @stdlib/array/byte-orders
25+
*
26+
* @example
27+
* var orders = require( '@stdlib/array/byte-orders' );
28+
*
29+
* var list = orders();
30+
* // e.g., returns [ 'little-endian', 'big-endian' ]
31+
*/
32+
33+
// MODULES //
34+
35+
var main = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = main;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var ORDERS = require( './orders.json' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Returns a list of byte orders.
30+
*
31+
* @returns {StringArray} list of byte orders
32+
*
33+
* @example
34+
* var list = orders();
35+
* // e.g., returns [ 'little-endian', 'big-endian' ]
36+
*/
37+
function orders() {
38+
return ORDERS.slice();
39+
}
40+
41+
42+
// EXPORTS //
43+
44+
module.exports = orders;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
"little-endian",
3+
"big-endian"
4+
]

0 commit comments

Comments
 (0)