• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

source-academy / js-slang / 15237418122

25 May 2025 11:31AM UTC coverage: 77.048% (-3.5%) from 80.538%
15237418122

push

github

web-flow
Rewrite: Stepper (#1742)

3433 of 4826 branches covered (71.14%)

Branch coverage included in aggregate %.

1032 of 1260 new or added lines in 27 files covered. (81.9%)

440 existing lines in 29 files now uncovered.

10099 of 12737 relevant lines covered (79.29%)

142411.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.93
/src/stdlib/misc.ts
1
import Closure from '../cse-machine/closure'
73✔
2
import { Context, Value } from '../types'
3
import { stringify } from '../utils/stringify'
73✔
4

5
/**
6
 * A function that displays to console.log by default (for a REPL).
7
 *
8
 * @param value the value to be represented and displayed.
9
 * @param externalContext a property of Context that can hold
10
 *   any information required for external use (optional).
11
 */
12
export function rawDisplay(value: Value, str: string, _externalContext: any) {
73✔
13
  // tslint:disable-next-line:no-console
14
  console.log((str === undefined ? '' : str + ' ') + value.toString())
1!
15
  return value
1✔
16
}
17

18
export function error_message(value: Value, ...strs: string[]) {
73✔
19
  const output = (strs[0] === undefined ? '' : strs[0] + ' ') + stringify(value)
2!
20
  throw new Error(output)
2✔
21
}
22

23
export function timed(
73✔
24
  context: Context,
25
  // tslint:disable-next-line:ban-types
26
  f: Function,
27
  externalContext: any,
28
  displayBuiltin: (value: Value, str: string, externalContext: any) => Value
29
) {
30
  return (...args: any[]) => {
×
31
    const start = get_time()
×
32
    const result = f(...args)
×
33
    const diff = get_time() - start
×
34
    displayBuiltin('Duration: ' + Math.round(diff) + 'ms', '', externalContext)
×
35
    return result
×
36
  }
37
}
38

39
export function is_number(v: Value) {
73✔
40
  return typeof v === 'number'
504✔
41
}
42

43
export function is_undefined(xs: Value) {
73✔
44
  return typeof xs === 'undefined'
11✔
45
}
46

47
export function is_string(xs: Value) {
73✔
48
  return typeof xs === 'string'
36✔
49
}
50

51
export function is_boolean(xs: Value) {
73✔
52
  return typeof xs === 'boolean'
26✔
53
}
54

55
export function is_object(xs: Value) {
73✔
56
  return typeof xs === 'object' || is_function(xs)
10✔
57
}
58

59
export function is_function(xs: Value) {
73✔
60
  return typeof xs === 'function'
1,729✔
61
}
62

63
export function is_NaN(x: Value) {
73✔
64
  return is_number(x) && isNaN(x)
4✔
65
}
66

67
export function has_own_property(obj: Value, p: Value) {
73✔
68
  return obj.hasOwnProperty(p)
2✔
69
}
70

71
export function is_array(a: Value) {
73✔
72
  return a instanceof Array
6✔
73
}
74

75
export function array_length(xs: Value[]) {
73✔
76
  return xs.length
25✔
77
}
78

79
/**
80
 * Source version of parseInt. Both arguments are required.
81
 *
82
 * @param str String representation of the integer to be parsed. Required.
83
 * @param radix Base to parse the given `str`. Required.
84
 *
85
 * An error is thrown if `str` is not of type string, or `radix` is not an
86
 * integer within the range 2, 36 inclusive.
87
 */
88
export function parse_int(str: string, radix: number) {
73✔
89
  if (
12✔
90
    typeof str === 'string' &&
47✔
91
    typeof radix === 'number' &&
92
    Number.isInteger(radix) &&
93
    2 <= radix &&
94
    radix <= 36
95
  ) {
96
    return parseInt(str, radix)
7✔
97
  } else {
98
    throw new Error(
5✔
99
      'parse_int expects two arguments a string s, and a positive integer i between 2 and 36, inclusive.'
100
    )
101
  }
102
}
103

104
export function char_at(str: string, index: number) {
73✔
105
  if (typeof str !== 'string') {
5✔
106
    throw new Error('char_at expects the first argument to be a string.')
1✔
107
  } else if (typeof index !== 'number' || !Number.isInteger(index) || index < 0) {
4✔
108
    throw new Error('char_at expects the second argument to be a nonnegative integer.')
2✔
109
  }
110
  return str[index]
2✔
111
}
112

113
/**
114
 * arity returns the number of parameters a given function `f` expects.
115
 *
116
 * @param f Function whose arity is to be found. Required.
117
 *
118
 * An error is thrown if `f` is not a function.
119
 */
120
export function arity(f: Function) {
73✔
121
  if (f instanceof Closure) {
9!
UNCOV
122
    const params = f.node.params
×
UNCOV
123
    const hasVarArgs = params[params.length - 1]?.type === 'RestElement'
×
UNCOV
124
    return hasVarArgs ? params.length - 1 : params.length
×
125
  } else if (typeof f === 'function') {
9✔
126
    return f.length
7✔
127
  } else {
128
    throw new Error('arity expects a function as argument')
2✔
129
  }
130
}
131

132
export function get_time() {
73✔
133
  return new Date().getTime()
16,730,150✔
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc