osscameroon / js-generator

Generates JavaScript from HTML
https://osscameroon.github.io/js-generator/
MIT License
18 stars 14 forks source link

what happens if we have a number of occurences that can not be represented with 3 digits ? #239

Closed FanJups closed 1 year ago

FanJups commented 1 year ago

What happens if we have a number of divs or other elements > 999 ? The program goes from 0 to 999 as you can see 03d. Should we not start at 001 instead of 000 ?

public class TypeBasedVariableNameStrategy implements VariableNameStrategy {
    private final Map<String, AtomicLong> counters = new HashMap<>();

    @Override
    public String nextName(@NonNull String type) {
        // NOTE: issue#145 careful with custom element about casing and dash, not to translate in JavaScript identifiers
        var identifier = type;
        final var HAS_DASH = type.contains("-");
        final var IS_ROOT = "targetElement".equals(type);
        final var HAS_UPPER_CASE = !type.chars()
                .allMatch(character -> Character.toLowerCase(character) == character);

        if (!IS_ROOT) {
            identifier = HAS_UPPER_CASE ? type.toLowerCase() : identifier;
            identifier = HAS_DASH
                    ? type.replaceAll("-", "_").replaceAll("_+", "_") : identifier;

            if (HAS_DASH || HAS_UPPER_CASE) {
                identifier = "custom_%s".formatted(identifier);
            }
        }

        return format("%s_%03d", identifier, counters.computeIfAbsent(type, __ -> new AtomicLong()).getAndIncrement());
    }
}
FanJups commented 1 year ago

https://github.com/osscameroon/js-generator/issues/240

FanJups commented 1 year ago

Finally, nothing to worry about. If we have more than 3 digits, the number is represented as it is. This %03d formatting doesnt break the code.