Skip to content

Commit

Permalink
Update some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnesfield committed Apr 6, 2024
1 parent aa4b76b commit a574e94
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions examples/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function run(args) {
) {
return;
}
// get index to slice off hyphens
const start =
arg.length > 1 && arg[1] !== '-'
? 1
Expand All @@ -50,16 +51,15 @@ function run(args) {
const object = { __proto__: null, _: root.args.slice() };
for (const node of root.children) {
const { args } = node;
const id = transform(node.id);
const id = camelCase(node.id);
if (node.id === '--' || id === '_') {
// handle positional args
object._ = Array.isArray(object._) ? object._ : [];
object._.push(...args);
} else if (args.length === 0) {
// handle boolean
const match = 'no-';
const negate = node.id.startsWith(match);
const prop = transform(negate ? node.id.slice(match.length) : node.id);
const prop = camelCase(negate ? node.id.slice(match.length) : node.id);
object[prop] = !negate;
} else if (args.length === 1 && args[0].includes(',')) {
// handle comma separated values
Expand All @@ -76,7 +76,7 @@ function run(args) {

// naive camelCase transform
/** @param {string | null} id */
function transform(id) {
function camelCase(id) {
if (typeof id !== 'string') {
return id;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function run(args) {
let curr = object;
for (const prop of props) {
read.push(prop);
if (!Object.prototype.hasOwnProperty.call(curr, prop)) {
if (!(prop in curr)) {
curr = curr[prop] = Object.create(null);
} else if (typeof curr[prop] === 'object' && curr[prop] !== null) {
curr = curr[prop];
Expand Down

0 comments on commit a574e94

Please sign in to comment.